Question

I am using jQuery.XDomainRequest.js (https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest) to support ajax requests in IE.

It works great for my GET request but the POST is not working, it returns a 500. (I checked, it works in other browsers so its XDR specific problem).

My ajax call looks like:

var contact = {};
contact.firstName = "ABC"; 

jQuery.ajax({
     type:  'POST',
     url:   contactDetailsUrl,
     data:  contact,
     dataType: 'json',
     success: function(data) {
                callback(true);
              },
     error: function(data) {
                alert('Currently having trouble posting.');
                callback(false);
            }
});

This is my 1st encounter with XDomainRequest, I did some research of my own, but I have no clue why is this failing.

Thanks in advance for your help!!!

EDIT: I did some debugging.

My test url = "http:/myserver/test/index.php?key=123" (This makes a cross-domain request)

I printed json_encode($_GET) , I get : {"key":"123"} I printed json_encodee($_POST), I get : [].

Clearly my post data is not being sent.

This is the line that sends my data:

xdr.send(postData);

postData looks somethings like:

"firstName=ABC"

My headers in the php code look like :

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
Was it helpful?

Solution

Ok, So my knowledge of php is worse than my XDR knowledge.

The link below helped

http://social.msdn.microsoft.com/Forums/eu/iewebdevelopment/thread/fe1570a0-8670-450d-90e6-1e03bd338569

I have to use $HTTP_RAW_POST_DATA instead of $_POST.

I'll leave the question up just incase there is someone else like me who stumbles upon this question.

OTHER TIPS

This should work for you in php:

echo file_get_contents('php://input');

To use it as $_POST:

$_POST=json_decode(file_get_contents('php://input'), true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top