2017-03-22 53 views
0

我試圖使用HTTP POST與離子2,但ionic2 HTTP POST不發送參數

import {Http, Headers, RequestOptions} from "@angular/http"; 
import 'rxjs/add/operator/map'; 

constructor(public navCtrl: NavController, public navParams: NavParams, public shareServices: ShareServices, public http: Http) { 
    this.data = {}; 
    this.data.email = ''; 
    this.data.password = '';  
} 

login() 
{ 
    let url = 'http://localhost/wp/ap/user/generate_auth_cookie/?insecure=cool'; 

    var headers = new Headers(); 
    headers.append("Accept", 'application/json'); 
    headers.append('Content-Type', 'application/x-www-form-urlencoded'); 

    let options = new RequestOptions({ headers: headers }); 
    let body = { 
     email : this.data.username, 
     password : this.data.password, 
    } 

    this.http.post(url,JSON.stringify(body), options).map(res => res.json()).subscribe(
    data => { 
     this.resp = data.json(); 
     console.log('RESP', this.resp); 
    }, 
    err => { 
     this.resp = err.json(); 
     console.log('ERROR', this.resp); 
    } 
); 
} 

我使用WordPress的用戶API插件

我不能將參數傳遞給服務器

https://wordpress.org/plugins/json-api-user/

+0

你有什麼錯誤? – Sampath

+0

沒有,沒有發現錯誤 – MSalah

回答

0

你能捕捉到任何錯誤嗎?也許你可以這樣試試:

let body = "username="+username+"&password="+password; 


      let headers = new Headers({ 
      'Accept': 'application/json', 
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 
      }); 
      let options = new RequestOptions({ 
       headers: headers, 
       body: body 
      }); 

      this.http.post(url, body, options) 
      .subscribe(
+0

謝謝你,這是爲我工作,但我怎樣才能得到響應的JSON – MSalah

+0

this.http.post(URL,身體,期權) \t \t .subscribe( \t \t \t響應=> { \t \t \t \t \t \t \t變種結果= response.json(); //結果是JSON對象 \t \t \t}, \t \t \t error => { \t \t \t \t console.log('error'); \t \t \t \t} \t \t); – coenni