我正在使用Yii框架作为服务器端,Angular JS作为客户端,开发一款RESTful应用程序。 我使用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