2017-03-05 9 views
0

私のサービスには、GetとPostという2つのメソッドがあります。リクエストを取得しようとしていますが、ポストリクエストが失敗したと言って失敗します。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); 
} 

取得:

enter image description here

ポスト:

enter image description here

私はポスト方法と間違って何をしているのですか?

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

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

答えて

2

角度のHttp.postメソッドの2番目のパラメータがないリクエストのオプションではなく、POSTリクエストのボディである:私は私の作品angular1アプリケーションから元のコードを持っています。

したがって、要求オプションを3番目のパラメータとしてhttp.postに渡し、2番目のパラメータではなく、呼び出しを変更する必要があります。

例:

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

see the Angular docs

+0

どのように私はその後、私の要求を変更することができますか? (this.base_url + 'save_progress /' + exerciseId +// "+ value +"/"、body:any、options)'これをどうすればいいですか? – Nitish

+0

this.http.post(this.base_url + 'save_progress /' + exerciseId + "/" + value + "/"、{}、options)を返します。あなたがボディに投稿する何も持っていないので、ちょうどあなたの2番目のパラメータとして空のオブジェクトを持ってください – snorkpete

+0

はいそれは働いた!指摘してくれてありがとう。 – Nitish

関連する問題