0
我的服務有兩個方法Get和Post。獲取請求正在工作,但發佈請求失敗,說未經授權。Angular2 http POST請求失敗
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;
}
如何修改,然後我的要求? '返回this.http.post(this.base_url +'save_progress /'+ exerciseId +「/」+ value +「/」,body:any,options)'這樣嗎? – Nitish
return this.http.post(this.base_url +'save_progress /'+ exerciseId +「/」+ value +「/」,{},options)會起作用。只是有一個空的物體作爲你的第二個參數,因爲你沒有任何東西在主體中發佈 – snorkpete
是的,它的工作!感謝您指出。 – Nitish