加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular2 OPTIONS方法发送时要求http.GET

发布时间:2020-12-17 07:41:37 所属栏目:安全 来源:网络整理
导读:参见英文答案 Why am I getting an OPTIONS request instead of a GET request?6 我试图添加 basic authentification到我的angular2应用程序. public login() { // Set basic auth headers this.defaultHeaders.set('Authorization','Basic ' + btoa(this.us
参见英文答案 > Why am I getting an OPTIONS request instead of a GET request?6
我试图添加 basic authentification到我的angular2应用程序.
public login() {
    // Set basic auth headers
    this.defaultHeaders.set('Authorization','Basic ' + btoa(this.username + ':' + this.password));

    console.log('username',this.username)
    console.log('password',this.password)
    console.log(this.defaultHeaders)

    // rest is copy paste from monbanquetapiservice
    const path = this.basePath + '/api/v1/development/order';        

    let req = this.http.get(path,{ headers: this.defaultHeaders });
    req.subscribe(
        _ => { },err => this.onError(err)
    );
}

我期望看到的是一个GET请求与我授权代理人.

但是我看到的是第一个这个标题的选项:

OPTIONS /api/v1/development/order HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/49.0.2623.110 Safari/537.36
Access-Control-Request-Headers: authorization,content-type
Accept: */*
Referer: http://localhost:3000/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6,fr;q=0.4

由于我的服务器不允许在这个URL上的OPTIONS,我收到一个错误.

我知道像PUT或POST这样的一些方法首先发送一个OPTIONS方法来预先请求请求,但是GET不会.

为什么angular2的http首先发送选项?

谢谢.

这是CORS工作的方式(使用跨域请求时).使用CORS,远程Web应用程序(这里是域mydomain.org)选择是否可以通过一组特定头文件来提供请求.

CORS规范区分了两个不同的用例:

>简单请求如果我们使用HTTP GET,HEAD和POST方法,则适用此用例.在POST方法的情况下,仅支持具有以下值的内容类型:text / plain,application / x-www-form-urlencoded和multipart / form-data.
>预检索请求当“简单请求”用例不适用时,将使用第一个请求(使用HTTP OPTIONS方法)来检查在跨域请求的上下文中可以执行的操作.

它不是Angular2发送OPTIONS请求,而是浏览器本身.这与Angular并不相关.

有关详细信息,请参阅本文:

> http://restlet.com/blog/2015/12/15/understanding-and-using-cors/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读