【问题标题】:Send data in a http post in angular 2?在 Angular 2 的 http 帖子中发送数据?
【发布时间】:2018-01-10 21:07:09
【问题描述】:

我正在尝试使用不同线程的 http post 发送数据,但我做不到。 我需要发送这些数据,在邮递员中测试过。

标题。 内容类型:application/x-www-form-urlencoded 授权:基本用户:pass

身体。 授予类型:密码 范围:个人资料

这是我的代码。

login() {

    let url = URL_LOGIN;

    let headers = new Headers(
      {
        'Content-Type': 'application/json',
        'Authorization': 'Basic user:pass'
      });

    let body = {
      'grant_type': 'password',
      'scope': 'profile'
    }

    return this.http.post(url, body, { headers: headers })
      .map((response: Response) => {
        var result = response.json();
        return result;
      })

  }

提前致谢!!

【问题讨论】:

    标签: javascript angular typescript ionic-framework http-headers


    【解决方案1】:

    有两点需要修改:

    1. 您传递到 http post 方法的标头错过了一步。它应包含以下内容:

      let options = new RequestOptions({ headers: headers });
      

      确保从@angular/http 导入RequestOptions

      然后将options 传递到您的 post 方法中,如下所示:

      return this.http.post(url, body, options)...
      
    2. http post 方法体只能是字符串。因此,应该如下:

      let body = 'grant_type=password' + '&scope=profile';
      

    【讨论】:

      猜你喜欢
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2019-10-05
      • 2015-12-30
      相关资源
      最近更新 更多