无法使用Ionic 2中的x-www-form-encoded调用post API

时间:2018-06-25 15:22:52

标签: api ionic-framework ionic2 content-type

我无法登录需要x-www-form-urlencoded标头的Ionic 2,我尝试了以下方式,但未成功;

login(username: any, password:any) {
    //First Way
    var body = JSON.stringify({username, password});
    //Second Way
    let urlSearchParams = new URLSearchParams();
    urlSearchParams.append("username",encodeURIComponent(username));//here
    urlSearchParams.append("password",encodeURIComponent(password));//and here
    let body = urlSearchParams.toString();

     //third way
     let postParams = {
      username: encodeURIComponent(username),
      password: encodeURIComponent(password)       
  }
    //fourth way
    var creds = "username=" + encodeURIComponent(username) + "&password=" + encodeURIComponent(password);
    //setting headers
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append("Accept", "application/json");
    let options = new RequestOptions({ headers: headers });
    let seq = this.api.post('authenticate', creds, options).share();

    seq.subscribe((res: any) => {
      // If the API returned a successful response, mark the user as logged in
      console.log(JSON.stringify(res));
      if (res.status == 'success') {
        this._loggedIn(res);
      } else {
      }
    }, err => {
      console.error('ERROR', err);
    });

0 个答案:

没有答案