2017-07-03 5 views
0

login.tsionic2フレームワーク(400 Badrequest)

onlogin() { 
    let login_email = "[email protected]"; 
    let login_password = "123456" 
    this.body = { 
     login_email: login_email, 
     login_password: login_password 
    }; 

    this.auth.login(this.body).subscribe(data => { 
     this.result = data 
     console.log(data); 
    }); 
} 

authservice.ts送信中

login(param) { 
    let headers = new Headers({ 'Content-Type': 'application/json' }); 
    let options = new RequestOptions({ headers: headers }); 
    return this.http.post(this.apiUrl + '/login', JSON.stringify(param), options) 
    .map((res: Response) => { 
     if (res) { 
      return { status: res.status, json: res.json() } 
     } 
    }); 
} 

方法は、郵便配達に正常に動作しているが、 ionic2からの同じリクエストがエラーを生成しています.Getメソッドはうまくいきましたが、postにいくつかの問題があります。リクエストは空のパラメータをポストに送り、エラー400を生成していません。

答えて

0

Login.ts

onlogin() {

const email = '[email protected]'; 
const password = '123456'; 
let body = new FormData(); 
body.append("login_email",email); 
body.append("login_password",password); 

     console.log(body); 
     this.auth.login(body).subscribe(data => {this.result = data; 
     this.navCtrl.push(HomePage,{result:this.result}); 
    }); 


} 

データを送信するためにFORMDATAを使用しauthservice.ts

login(param) 
    { 
    const headers = new Headers({'ContentType':'application/formdata'}); 
    return this.http.post('/api',param,headers) 
       .map(this.extractDataLogin)  
       .catch(error => this.handleError(error)); 
    } 

はい、私が使用してでこれを試してみました

let body = new FormData(); 
body.append("login_email",email); 
body.append("login_password",password); 
0

リクエストをデバッグしましたが、空のボディが送信されていますか?これは実際に違いがあるかどうかはわかりませんが、JSON.stringify()を使用せずにpost()メソッドに直接サーバーに送信する必要があるオブジェクトを2番目のパラメータとして渡します。既にその部分を省略しようとしましたか?

+0

私の問題を解決json.stringify、私はthを得た同じエラー。catchブロックを使用してエラーを処理すると、 "TypeError:ストリームが予期されていた場所で無効なオブジェクトを指定しました。 Observable、Promise、Array、またはIterableを提供することができます。 – user2215155

関連する問題