我在 Firefox 上遇到 AngularJS 1.08 和 CORS 的奇怪问题。我的 AngularJS 看起来像这样:
var MyApp = angular.module('MyApp');
MyApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);
$http({
dataType : 'json',
url : 'http://api.example.com/restful',
withCredentials : true,
method: 'POST',
data : {val1 : 'Test'}
}).success(function(response) { console.log(response)}).error(function(response) { console.log(response)});
PHP 端看起来像这样:
header('Access-Control-Allow-Origin: '. $_SERVER['HTTP_ORIGIN'] );
header('Access-Control-Allow-Credentials: true' );
header('Access-Control-Request-Method: *');
header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: *,x-requested-with,Content-Type');
header('X-Frame-Options: DENY');
if(strtolower($_SERVER['REQUEST_METHOD']) == 'get') {
//DO XYZ
} else if (condition) {
//DO DEF
}
这在 Chrome 和 Safari 以及 FireFox 上工作正常 $_GET 请求有效但 $_POST 无效。在 Firefox 和 Apache 上,我收到错误 500,在 Nginx 上,它在没有数据的情况下执行,并抛出一条空错误消息。我的 CORS 设置有问题吗?
最佳答案
$_POST
和 $_GET
不起作用,您应该使用此代码读取从 Angular 发送的数据
$data = file_get_contents("php://input");
$objData = json_decode($data);
我强烈建议您查看 AngularPHP , 省去一些麻烦
关于javascript - AngularJS PHP CORS 不适用于 FireFox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19095707/