javascript - AngularJS PHP CORS 不适用于 FireFox

标签 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.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/

相关文章:

php - 无法在php中显示任何错误

javascript - Angular nvd3折线图长度

AngularJS UI-Router 从 View 中获取当前状态

javascript - React-router 将 POST 请求添加到 URL 前面

javascript - 替换 Javascript 字符串中的双引号和单引号

javascript - 将 HTML 表单中的值提交到 Node-js

php - wordpress 小部件不会保存

php - 限制两列之和的结果

javascript - SPA 中的 ADAL JS 强制注销

javascript - 使用 Zippopotam.us 的 JSON 结果作为 Google map 坐标