[英]OPTIONS request is not firing with http call through Angular2 Http
I am playing a bit with Angular2 structure and I got to the point where I want to pull information from the server. 我在玩Angular2结构,到了要从服务器提取信息的地步。 Now, my api's domain is different from the FrontEnd app, and I am expecting that the browser will fire OPTIONS
request before executing the actual one. 现在,我的api的域与FrontEnd应用程序不同,并且我希望浏览器在执行实际的请求之前会触发OPTIONS
请求。 However, that is not happening. 但是,这没有发生。 Instead, what I get is an error: 相反,我得到的是一个错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/rrm/api/v1/goals . 跨域请求被阻止:同源策略禁止读取位于http:// localhost:8080 / rrm / api / v1 / goals的远程资源。 (Reason: CORS header 'Access-Control-Allow-Origin' missing). (原因:CORS标头“ Access-Control-Allow-Origin”缺失)。
And my network log looks like this: 我的网络日志如下所示:
My dead simple Angular2 code is as follows: 我死了的简单Angular2代码如下:
export class AppComponent implements OnInit {
goals: Object[];
constructor(public authHttp: AuthHttp) {}
ngOnInit():any {
this.getGoals();
}
getGoals() {
this.authHttp.get('http://localhost:8080/rrm/api/v1/goals')
.subscribe(
data => this.goals = data.arrayBuffer(),
err => console.log('Error ', err),
() => console.log('Request Complete')
);
}
}
What am i missing? 我想念什么? I am not getting options
request on the server and I don't see it in the browser... 我没有在服务器上收到options
请求,并且在浏览器中也看不到...
Thanks, 谢谢,
In fact, there are two main cases in CORS: 实际上,CORS中有两种主要情况:
Simple requests . 简单的要求 。 We are in this case if we use HTTP methods GET
, HEAD
and POST
. 在这种情况下,如果我们使用HTTP方法GET
, HEAD
和POST
。 In the case of POST
method, only content types with following values are supported: text/plain
, application/x-www-form-urlencoded
, multipart/form-data
. 对于POST
方法,仅支持具有以下值的内容类型: text/plain
, application/x-www-form-urlencoded
, multipart/form-data
。 Even if you're in these case and if you use a custom header (a header that you define by your own in your request), you'll fall into the preflighted request. 即使您遇到这种情况,并且使用自定义标头(您在请求中自行定义的标头),也会陷入预检请求中。
Preflighted requests . 事前要求 。 When you aren't in the case of simple requests, a first request (with HTTP method OPTIONS
) is done to check what can be done in the context of cross-domain requests. 如果不是简单请求,则执行第一个请求(使用HTTP方法OPTIONS
)以检查在跨域请求的上下文中可以执行的操作。
In your case, you're in the first case (simple request). 就您而言,您处于第一种情况(简单请求)。
Moreover your GET request should define a Access-Control-Allow-Origin
header in its response not to have the error. 此外,您的GET请求应在其响应中定义一个Access-Control-Allow-Origin
标头,以免出现错误。 This will allow the browser to determine if you're (localhost:3000 for example) able to execute the request. 这将使浏览器确定您是否能够执行请求(例如localhost:3000)。
This article could interest you: 本文可能使您感兴趣:
The OPTIONS
is only sent befor every POST
request. 仅在每个POST
请求之前发送OPTIONS
。 For GET
requests an OPTIONS
preflight request is only sent if you have custom headers like auth-headers. 对于GET
请求,仅当您具有自定义标头(如auth-headers)时,才发送OPTIONS
预检请求。
See also Why am I getting an OPTIONS request instead of a GET request? 另请参见为什么我收到OPTIONS请求而不是GET请求?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.