在Chrome浏览器中,如果想通过AJAX请求不同域中的资源,很可能就会遇到 “Origin null is not allowed by Access-Control-Allow-Origin” 的错误。
这是因为在新版本浏览器中安全策略引起。简单的说,如果XMLHttpRequest 请求的URL和当前页面不同一个域中时,浏览器会检测响应http header中有没有 Access-Control-Allow-Origin项,如果此项值为空或者与当前页面的域不匹配时,就会报此错误。
W3C标准中有个叫Cross Origin Resource Sharing (CORS)的东东用来解决此问题。
简单的说在目标资源返回的http头中需要包含有“Access-Control-Allow-Origin”项,并且其值格需要能匹配请求源页面域。
如在 http://a.com/default.html 页面中通过 ajax 获取 http://b.com/demo.aspx 页面的内容,那么响应http header中Access-Control-Allow-Origin就必须能匹配 http://a.com,否则就会出现以上错误。
添加此 http header的方法有几种:
$.ajax, $.post, $.get访问远程地址时可能报错:
XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin
这是JS跨域访问产生的问题,解决办法要修改远程源代码:
1.请求的url指向PHP页面时,在PHP中添加 header("Access-Control-Allow-Origin: *");
2.html时,添加 <meta http-equiv="Access-Control-Allow-Origin" content="*">
相关推荐
当使用ajax跨域请求时,浏览器报错:XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin.肯定是跨域的问题,如果用jsonp或者proxy的方式进行修改的话未免需要太大的工程量,所以采用...
Access to XMLHttpRequest at'http://localhost:跨域问题解决
Origin 'null' is therefore not allowed access. 这就是跨域问题。解决方案有不少,比较好的是服务器端配置CORS,但要求服务器端做更改。如果在不需要更改服务器端的情况下解决呢?尤其是需要在
Origin [...] is not allowed by Access-Control-Allow-Origin. XMLHttpRequest cannot load. Origin [...] is not allowed by Access-Control-Allow-Origin. 非常占用 CPU:高分辨率时缓慢,滞后很快。 可能的...
默认值:Access-Control-Allow-Origin:'*'Access-Control-Allow-Methods:'GET,PUT,POST,DELETE,HEAD,OPTIONS,PATCH'要报告错误,请使用:https://github.com / balvin-perrie / Access-Control-Allow-Origin...
1、 通过向浏览器中安装指定的插件,解决跨域问题:Access to XMLHttpRequest at ‘http://localhost:8080/xxx‘ No ‘Access-Control-Allow-Origin‘ head_一觉睡过头的菜鸡的博客-CSDN博客_access to ...
No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://当前页的域名’ is therefore not allowed access. 2、为什么会出现跨域问题 因为浏览器收到同
服务器可以根据这个头来判断是否允许该请求,并返回Access-Control-Allow-Origin头来表明允许的域名。 如果请求不是简单请求,浏览器将首先发送一个预检请求(preflight request),以确定服务器是否允许该请求。...
header("Access-Control-Allow-Origin:*"); /*星号表示所有的域都可以接受,*/ header("Access-Control-Allow-Methods:GET,POST"); 3.jsonP 原理: ajax本身是不可以跨域的, 通过产生一个script标签来实
今天用execjs调用JS时,发现报错execjs._exceptions.ProgramError: ReferenceError: document is not defined 如图: 错误类型:文档对象未定义,解决方法,定义对象即可,习惯性定义变量嘛 var document ;? ...
依赖于CCL,...报XMLHttpRequest cannot load 和No 'Access-Control-Allow-Origin' header is present on the requested resource.的错误 暂时无解,我在服务器上已经enable了CORS
新的W3C策略实现了HTTP跨域访问,还亏我找了很久的资料解决这个问题:只需要在servlet中返回的头部信息中添加Access-Control-Allow-Origin这个既可。比如我要开放所有我本地的跨域访问,就设置如下:response.set...
凭什么? 什么是凭证? 如果为true,则可以在不同端口之间共享cookie ... The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's
XMLHttpRequest cannot load http://192.168.0.118:8081/get_mobile_number/?id=1. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘null’ is therefore not
XMLHttpRequest cannot load http://192.168.2.12:8001/oss/api/version/check. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8000’ is therefor
Access-Control-Request-Headers 首部字段告知服务器实际请求所携带的自定义首部字段。服务器基于从预检请求获得的信息来判断,是否接受接下来的实际请求。览器先询问服务器,当前网页的域名是否在服务器的许可名单...
概述:此扩展仅覆盖 XMLHTTPRequest 对象中的响应数据以及 fetch 方法。 描述: .建议在不使用时关闭此扩展程序(图标应为灰色)。 2.此扩展仅覆盖XMLHTTPRequest对象中的响应数据以及fetch方法。您可以在DevTools的...
在源网页加载之前,实现一个XMLHttpRequest的代理对象,然后覆盖全局的XMLHttpRequest,这样一但上层调用 new XMLHttpRequest这样的代码时,其实创建的是Ajax-hook的代理对象实例
XMLHttpRequest cannot load http://server.runoob.com/server.php. No ‘Access-Control-Allow-Origin’ header is present on the requested resource.Origin ‘http://client.runoob.com’ is theref