【发布时间】:2014-10-24 06:54:18
【问题描述】:
我正在使用 Yii 框架为服务器端和 Angular JS 为客户端开发一个 REST-ful 应用程序
我正在使用restfulyii扩展来生成api
:我在发送 PUT 请求时遇到了问题。
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ..... This can be fixed by moving the resource to the same domain or enabling CORS.
但它适用于 post + get 请求
我看到了不同的解决方案,但都没有奏效。
我试着把那些放在服务器端
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, authorization, accept, client-security-token");
header("Access-Control-Max-Age: 1000");
并尝试将此代码放入 Angular js 模块中:
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
我也试着把
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
请求转化为OPTIONS请求 来自服务器的响应如下:
Access-Control-Allow-Headers:x-requested-with, Content-Type, origin, authorization, accept, client-security-token
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS Access-Control-Allow-
Origin:http://localhost:8383
Access-Control-Max-Age:1000
Connection:close
Content-Type:text/html Date:Fri, 24 Oct 2014 06:49:32 GMT
Server:Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.9 X-Powered-By:PHP/5.5.9
【问题讨论】:
-
尝试将确切的域名而不是 * 放入 header("Access-Control-Allow-Origin: *");
-
但是当我使用邮递员扩展发送确切的请求时,它可以工作!
-
你是如何加载你的 Angular 网站的?你使用的是 file:// 还是 http:// 协议?
标签: javascript angularjs yii