简体   繁体   English

AngularJS没有检测到Access-Control-Allow-Origin标头?

[英]AngularJS not detecting Access-Control-Allow-Origin header?

I am running an angular app on a local virtualhost ( http://foo.app:8000 ). 我正在本地虚拟主机( http://foo.app:8000 )上运行有角度的应用程序。 It is making a request to another local VirtualHost ( http://bar.app:8000 ) using $http.post . 它正在使用$http.post向另一个本地VirtualHost( http://bar.app:8000 )发出请求。

$http.post('http://bar.app:8000/mobile/reply', reply, {withCredentials: true});

In the Network tab of Chrome Developer Tools I of course see the OPTIONS request, and the response includes the header: 我当然会在Chrome开发者工具的“网络”标签中看到OPTIONS请求,并且响应中包含标头:

Access-Control-Allow-Origin: http://foo.app:8000

However, the POST request is cancelled with the following error: 但是,POST请求被取消,并出现以下错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://foo.app:8000 ' is therefore not allowed access. 因此,不允许访问源' http://foo.app:8000 '。

Has anyone experienced this? 有人经历过吗? The Access-Control-Allow-Origin header is very plainly included in the response of the OPTIONS request, so I can't for the life of me figure out why the POST is acting the header was missing. OPTIONS请求的响应中非常清楚地包含了Access-Control-Allow-Origin标头,因此我一辈子都无法弄清为什么POST充当标头而丢失了。

Access-Control-Allow-Credentials is also set to true . Access-Control-Allow-Credentials也设置为true

It's a bug in chrome for local dev. 对于本地开发人员来说,这是Chrome中的错误。 Try other browser. 尝试其他浏览器。 Then it'll work. 这样就可以了。

There's a workaround for those who want to use Chrome. 对于想要使用Chrome的用户,有一种解决方法。 This extension allows you to request any site with AJAX from any source, since it adds 'Access-Control-Allow-Origin: *' header to the response. 此扩展名允许您从任何来源请求使用AJAX的任何站点,因为它在响应中添加了'Access-Control-Allow-Origin: *'头。

As an alternative, you can add this argument to your Chrome launcher: --disable-web-security . 或者,您可以将此参数添加到Chrome启动器中:-- --disable-web-security Note that I'd only use this for development purposes, not for normal "web surfing". 请注意,我只会将其用于开发目的,而不能用于常规的“网络冲浪”。 For reference see Run Chromium with Flags . 有关参考,请参见使用标志运行Chromium

As a final note, by installing the extension mentioned on the first paragraph, you can easily enable/disable CORS. 最后,通过安装第一段中提到的扩展,您可以轻松启用/禁用CORS。

I was sending requests from angularjs using $http service to bottle running on http://localhost:8090/ and I had to apply CORS otherwise I got request errors like "No 'Access-Control-Allow-Origin' header is present on the requested resource" 我正在使用$ http服务从angularjs发送请求到运行在http://localhost:8090/上的瓶子的请求,并且我必须应用CORS否则我会收到请求错误,例如“在服务器上没有'Access-Control-Allow-Origin'标头要求的资源”

from bottle import hook, route, run, request, abort, response

#https://github.com/defnull/bottle/blob/master/docs/recipes.rst#using-the-hooks-plugin

@hook('after_request')
def enable_cors():
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT'
    response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'

I experienced this exact same issue. 我遇到了同样的问题。 For me, the OPTIONS request would go through, but the POST request would say "aborted." 对我来说,OPTIONS请求会通过,但POST请求会说“已中止”。 This led me to believe that the browser was never making the POST request at all. 这使我相信浏览器根本不会发出POST请求。 Chrome said something like "Caution provisional headers are shown" in the request headers but no response headers were shown. Chrome在请求标头中说了类似“显示警告临时标头”的内容,但未显示响应标头。 In the end I turned to debugging on Firefox which led me to find out my server was responding with an error and no CORS headers were present on the response. 最后,我转向在Firefox上进行调试,这使我发现服务器正在响应并显示错误,并且响应中没有CORS标头。 Chrome was actually receiving the response, but not allowing the response to be shown in the network view. Chrome实际上是在收到响应,但不允许在网络视图中显示响应。

CROS needs to be resolved from server side. CROS需要从服务器端解决。

Create Filters as per requirement to allow access and add filters in web.xml 根据要求创建过滤器以允许访问并在web.xml中添加过滤器

Example using spring: 使用spring的示例:

Filter Class: 过滤器类别:

@Component
public class SimpleFilter implements Filter {

@Override
public void init(FilterConfig arg0) throws ServletException {}

@Override
public void doFilter(ServletRequest req, ServletResponse resp,
                     FilterChain chain) throws IOException, ServletException {

    HttpServletResponse response=(HttpServletResponse) resp;

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");

    chain.doFilter(req, resp);
}

@Override
public void destroy() {}

}

Web.xml: Web.xml:

<filter>
    <filter-name>simpleCORSFilter</filter-name>
    <filter-class>
        com.abc.web.controller.general.SimpleFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>simpleCORSFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

I just ran into this problem today. 我今天刚遇到这个问题。 It turned out that a bug on the server (null pointer exception) was causing it to fail in creating a response, yet it still generated an HTTP status code of 200. Because of the 200 status code, Chrome expected a valid response. 事实证明,服务器上的一个错误(空指针异常)导致其无法创建响应,但仍生成HTTP状态代码200。由于存在200状态代码,Chrome期望有效的响应。 The first thing that Chrome did was to look for the 'Access-Control-Allow-Origin' header, which it did not find. Chrome所做的第一件事是寻找“ Access-Control-Allow-Origin”标头,但找不到。 Chrome then cancelled the request, and Angular gave me an error. Chrome然后取消了该请求,Angular给了我一个错误。 The bug during processing the POST request is the reason why the OPTIONS would succeed, but the POST would fail. 处理POST请求期间的错误是OPTIONS成功但POST失败的原因。

In short, if you see this error, it may be that your server didn't return any headers at all in response to the POST request. 简而言之,如果您看到此错误,则可能是您的服务器根本没有返回任何标头来响应POST请求。

It can also happen when your parameters are wrong in the request. 当您的参数在请求中不正确时,也会发生这种情况。 In my case I was working with a API that sent me the message 就我而言,我正在使用向我发送消息的API

"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 401." “所请求的资源上不存在'Access-Control-Allow-Origin'标头。因此,不允许访问源'null'。响应的HTTP状态码为401。”

when I send wrong username or password with the POST request to login. 当我用POST请求发送错误的用户名或密码登录时。

Instead of using $http.get ('abc/xyz/getSomething') try to use $http.jsonp ('abc/xyz/getSomething') 而不是使用$ http.get( 'ABC / XYZ / getSomething')的尝试使用$ http.jsonp( 'ABC / XYZ / getSomething')

     return{
            getList:function(){
                return $http.jsonp('http://localhost:8080/getNames');
            }
        }

If you guys are having this problem in sails.js just set your cors.js to include Authorization as the allowed header 如果你们在sails.js中遇到此问题,只需将cors.js设置为包括Authorization作为允许的标头

 /*************************************************************************** * * * Which headers should be allowed for CORS requests? This is only used in * * response to preflight requests. * * * ***************************************************************************/ headers: 'Authorization' // this line here 

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

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