I have this jQuery script that sends a username an password to a PHP file which checks if this user exists and returns a JSON user object. Works fine in non-IE browsers, but IE fails.
The wierd thing is that the IE "firebug" says everything is fine, but the PHP script doesnt recieve any vars...
This is the request body:
username=johanderowan&password=1234
These are the request headers (I left out a few vars for security reasons):
Request = POST /1.0/account/login.json HTTP/1.1
Accept = /
Origin = [DEVURL]
Accept-Language = nl-NL
UA-CPU = AMD64
Accept-Encoding = gzip, deflate
User-Agent = Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
Host = [DEVURL]
Content-Length = 66
Connection Keep-Alive Cache-Control no-cache
The response body is (first three empty arrays are $_GET, $_POST and $_REQUEST):
Array ( )
Array ( )
Array ( )
{"status":"error","message":"No username or password specified.","httpCode":500}
This is the request script:
$('.mobyNowLoginForm form').bind('submit', function(){
var username = $(this).find('.username').val();
var password = $(this).find('.password').val();
$.post('[url]/1.0/account/login.json', {
username: username,
password: password
}, function(response) {
// do something
}, "JSON");
return false;
});
I have no clue at all what could be wrong here...
It appears that IE doesn't send a correct conent-type in cross-domain requests. The content-type is always set to "text/plain".
Read more about this shortcoming in this blogpost: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
We fixed this on the server by parsing the php://input string and setting this as $_POST vars.
if ($_SERVER['REQUEST_METHOD'] == 'POST' && count($_POST) == 0) {
$postData = file_get_contents('php://input');
$postVars = explode('&', $postData);
foreach ($postVars as $postVar) {
list($key, $var) = explode('=', $postVar);
$_POST[$key] = $var;
}
}
Have you tried to set cache to false. I've had a similar problem and this fixed it:
$.ajax({
type: 'POST',
url: '[url]/1.0/account/login.json',
dataType: 'json',
data: {
username: username,
password: password
},
cache: false,
success: function() {
// Do something
}
});
I hope this helps you!
Steffen
Edit
The problem could also be in your request URL. Your trying to make a call to a .json file. You could try to make a call to a .php file instead.
Make sure you put this in your .php file:
header("Content-Type: application/json");