js跨域访问,No ‘Access-Cont…

最近在用cocos2d-js做游戏时,

在本地用ajax跨域访问请求时报错:


XMLHttpRequest cannot loadhttp://www.zjblogs.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.


在网上查了下,解决办法如下:


1、如果请求的url是aspx页面,则需要在aspx页面中添加代码:Response.AddHeader("Access-Control-Allow-Origin", "*");


2、如果请求的url是PHP页面,则需要在PHP页面中添加代码:header("Access-Control-Allow-Origin: *");


3、如果请求的url是静态的html页面,则需要在页面中添加meta标签代码:


这里的*是代表允许所有域名访问,如果服务器端可以确定是要被哪些域名访问,最好是能把以上代码中的“*”代替为具体的域名,这样做可以相应的增强安全性。

猜你喜欢

转载自blog.csdn.net/zhanqixuan22/article/details/51545421