简体   繁体   English

在角度js中添加标头时,HTTP GET请求更改为OPTIONS

[英]HTTP GET request changes into OPTIONS while adding headers in angular js

Following is the request i used so far 以下是我目前使用的请求

$http.get(url)
    .success(function (data){})
    .error(function (data){})

works without any CORS issues. 没有任何CORS问题。 My server side Allows all origins, methods, all headers 我的服务器端允许所有来源,方法,所有标题

when i add http header like 当我添加http标头时

$http.get(url, { headers: { "USERID": user,  "SESSIONID": sessionId}})

the request changes into OPTIONS method when i see in chrome dev tools network tab 当我在chrome dev工具网络选项卡中看到时,请求会更改为OPTIONS方法

What is the reason for this? 这是什么原因? if it is expected then how to add custom http headers. 如果它是预期的,那么如何添加自定义http标头。

I have gone thru this link angularjs-performs-an-options-http-request-for-a-cross-origin-resource but it didnt help 我已经通过这个链接angularjs-perform-an-options-http-request-for-a-cross-origin-resource但它没有帮助

Here i am expecting that server should allow different origins . 在这里,我期待服务器应该允许不同的起源 But it is allowing headers, only if i were in a same server. 但它允许标题,只有我在同一台服务器。 But not sure about this is by angular or by server side. 但不确定这是通过角度还是服务器端。

after headers 标题后

$http.get(url,{ headers: { "USERID": user, "SESSIONID": sessionId } }) $ http.get(url,{headers:{“USERID”:user,“SESSIONID”:sessionId}})

in chrome dev tools i am seeing like 在我看到的chrome dev工具中

Request Method:OPTIONS
Status Code:404 Not Found

but without headers 但没有标题

Request Method:GET
Status Code:200 OK

When i do this in REST Client, i can send headers to the backend. 当我在REST Client中执行此操作时,我可以将标头发送到后端。

$http({method: 'GET', url: '/someUrl', headers: { "USERID": user,  "SESSIONID": sessionId}}).
success(function(data, status, headers, config) {
  // this callback will be called asynchronously
  // when the response is available
}).
error(function(data, status, headers, config) {
  // called asynchronously if an error occurs
  // or server returns response with an error status.
});

will work. 将工作。 $http.get is a shortcut method. $ http.get是一种快捷方法。 Check the config in the docs 检查文档中config

This is a known bug, see for instance https://github.com/angular/angular.js/issues/1585 . 这是一个已知的错误,请参阅https://github.com/angular/angular.js/issues/1585

A workaround is to use a jQuery request. 解决方法是使用jQuery请求。

I had the same massive issue when trying to pass header in my get, where it changes get to options and wouldn't work. 当我尝试在我的get中传递header时,我遇到了同样的大问题,在那里它改变了选项并且不起作用。 In order to make it work I added the following in my php api 为了使它工作,我在我的php api中添加了以下内容

<?php if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {  
  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'GET') {    
    header("Access-Control-Allow-Headers: Authorization, X-Auth-Token");    
  }
  exit;
} ?>

You can allow for any headers that you wish to pass. 您可以允许您希望传递的任何标头。 Hope this helps 希望这可以帮助

For my particular problem with my C# Web API solution I had to have something handle the Options request. 对于我的C#Web API解决方案的特殊问题,我必须处理Options请求。 Angular was sending a preflight request method OPTIONS which I did allow in my web.config with Angular正在发送一个预检请求方法OPTIONS,我在web.config中允许这个方法

  <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS, PATCH" />

But that wasn't enough I also included a method to handle the Options Request and I returned nothing 但这还不够,我还包括一个处理选项请求的方法,但我没有返回任何内容

[ResponseType( typeof( void ) )]
public IHttpActionResult OptionsPost() {
  return StatusCode( HttpStatusCode.NoContent );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM