Angular2 http POST请求失败

时间:2017-03-05 20:50:36

标签: angular django-rest-framework ionic2

我的服务有两种方法Get和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);
} 

获取:

enter image description here

发表:

enter image description here

Post方法有什么问题?我的angular1应用程序的原始代码有效:

var url = "/api/exercises/save_progress/" + exerciseType + "/" + rating + "/";

              if($cookies.token){
                  $http.defaults.headers.common.Authorization = 'Token ' + $cookies.token;
              }

1 个答案:

答案 0 :(得分:2)

angular的Http.post方法的第二个参数不是请求选项,而是post请求的主体。

因此,您应该更改您的调用,将您的请求选项作为第三个参数传递给http.post,而不是第二个参数。

例如:

return this.http.post(this.base_url + 'save_progress/' + exerciseId + "/" + value + "/", {}, options) 

see the Angular docs

相关问题