Reputation: 1594
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');
Upvotes: 1
Views: 3641
Reputation: 300
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);
Upvotes: 1
Reputation: 1594
Ok, So my knowledge of php is worse than my XDR knowledge.
The link below helped
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.
Upvotes: 1