Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.post sends vars correct, but PHP receives nothing

Tags:

jquery

post

php

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...

like image 570
Dreamdealer Avatar asked Jan 22 '26 13:01

Dreamdealer


1 Answers

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;
    }
}
like image 68
Dreamdealer Avatar answered Jan 25 '26 08:01

Dreamdealer