【问题标题】:AngularJS PHP CORS not working with FireFoxAngularJS PHP CORS 不适用于 FireFox
【发布时间】:2013-10-06 09:41:54
【问题描述】:

我在 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 设置有问题吗?

【问题讨论】:

    标签: javascript php angularjs apache


    【解决方案1】:

    $_POST$_GET 不起作用,您应该使用此代码读取从 angular 发送的数据

                    $data = file_get_contents("php://input");
                    $objData = json_decode($data);
    

    我强烈建议查看AngularPHP,并省去一些麻烦

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 2016-01-15
      • 2015-02-21
      • 1970-01-01
      • 2019-12-23
      • 2018-02-01
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      相关资源
      最近更新 更多