Javascript AngularJS PHP CORS不适用于FireFox
我对Firefox上的AngularJS 1.08和CORS有一个奇怪的问题。我的AngularJS看起来像这样:Javascript AngularJS PHP CORS不适用于FireFox,javascript,php,angularjs,apache,Javascript,Php,Angularjs,Apache,我对Firefox上的AngularJS 1.08和CORS有一个奇怪的问题。我的AngularJS看起来像这样: var MyApp = angular.module('MyApp'); MyApp.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.c
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)});
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
}
PHP方面如下所示:
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)});
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上运行良好$\u GET request可以正常工作,但不能在$\u POST上运行。在Firefox和Apache上,我得到一个错误500,在Nginx上,它执行时没有数据,并抛出一条空的错误消息。我的CORS设置有问题吗?
$\u POST
和$\u GET
不起作用,您应该使用此代码读取从CORS发送的数据
$data = file_get_contents("php://input");
$objData = json_decode($data);
我强烈建议你去看看,省去一些头疼的事