【问题标题】:how to make post request with oAuth2 token authentication如何使用 oAuth2 令牌身份验证进行发布请求
【发布时间】:2018-07-22 08:34:51
【问题描述】:

此代码无效:

createRoute(FormRoute) {
    var body = "&name=" + FormRoute.name + "&description="+ FormRoute.description ;
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded' );
    headers.append('Authorization',  'oauth ' + localStorage.getItem("access_token"));
    return new Promise((resolve, reject) => {
        this.http.post('url', body, {headers: headers})
        .toPromise()
        .then((response) =>
        {
            var res = response.json();
            console.log(res);
            resolve(response.json());
        })
        .catch((error) =>
        {
            var err = error.json();
            console.error("err");
            console.error(err["error_description"]);
            let alert = this.alert.create({
              title: 'Error',
              subTitle: err["error_description"],
              buttons: ['OK']
            });
            alert.present();
        });
    });
}

OPTIONS 'http://url' 401(未授权)

未能加载“http://url”:预检响应包含无效的 HTTP 状态代码 401。

【问题讨论】:

  • 需要在服务器端配置CORS。
  • 什么意思
  • 研究一下CORS你就会明白了。
  • 我在 chrome 上安装 CROS,这不是问题。问题是如何在 post 请求的标头中发送令牌 oauth2
  • 问题是CORS。预检选项 401 始终是 CORS:stackoverflow.com/questions/42221602/…

标签: javascript angular typescript ionic-framework ionic2


【解决方案1】:

我相信下面这行代码是这里的罪魁祸首。

this.http.post('url', body, {headers: headers})

我认为您不会尝试向http://url 发帖,因为这没有任何意义。

改成下面的。

this.http.post(url, body, {headers: headers})

在某处定义您的网址:

var url = 'http://httpbin.org/'

希望这会有所帮助。

【讨论】:

  • 我像你一样添加了 url 我得到了错误(索引):1 无法加载 http://.../api/:预检响应具有无效的 HTTP 状态代码 401。
猜你喜欢
  • 2018-11-12
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 2019-08-07
  • 2016-05-30
  • 2020-01-10
  • 2021-05-12
  • 2018-11-14
相关资源
最近更新 更多