[英]Angular2 http POST request failing
我的服務有兩種獲取和發布方法。 獲取請求有效,但發布請求失敗,提示未授權。
public getExercise(exerciseId): Observable<Exercise[]>{
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers, withCredentials: true });
return this.http.get(this.base_url + 'get_exercise/' + exerciseId + '/', options)
.map(this.extractData)
.catch(this.handleError);
}
public saveRating(value, exerciseId): Observable<Exercise[]>{
console.log(value + " " + exerciseId)
let headers = new Headers({ 'Content-Type': 'application/json' });
headers.append('Authorization','Basic')
let options = new RequestOptions({ headers: headers, withCredentials: true });
return this.http.post(this.base_url + 'save_progress/' + exerciseId + "/" + value + "/", options)
.map(this.extractData)
.catch(this.handleError);
}
得到:
發布:
Post方法有什么問題? 我從我的angular1應用程序中獲得了原始代碼,該代碼可以正常工作:
var url = "/api/exercises/save_progress/" + exerciseType + "/" + rating + "/";
if($cookies.token){
$http.defaults.headers.common.Authorization = 'Token ' + $cookies.token;
}
angular的Http.post方法的第二個參數不是請求選項,而是發布請求的正文。
因此,您應該更改呼叫以將請求選項作為第三個參數傳遞給http.post,而不是第二個。
例如:
return this.http.post(this.base_url + 'save_progress/' + exerciseId + "/" + value + "/", {}, options)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.