ionic2 http post不发送参数

时间:2017-03-22 21:32:17

标签: ionic2

我正在尝试使用带有离子2的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/

1 个答案:

答案 0 :(得分: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(