3

I'm having a weird problem with AngularJS 1.08 and CORS on Firefox. My AngularJS looks like this:

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)});

The PHP side looks like this:

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
}

This working fine on Chrome and Safari and on FireFox $_GET request works but not $_POST. On Firefox and Apache I get an Error 500 and on Nginx it exectues without the data, and throws an empty error message. Is there something wrong with my setup for CORS?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Devin Dixon
  • 11,553
  • 24
  • 86
  • 167

1 Answers1

0

$_POST and $_GET don't work, you should use this code to read the data sent from angular

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

I highly recommend to take a look into AngularPHP, and save some headaches

Uri Goren
  • 13,386
  • 6
  • 58
  • 110