dongyan1548 2012-05-23 13:53
浏览 51
已采纳

jQuery $ .post发送vars正确,但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...

  • 写回答

2条回答 默认 最新

  • duanbi3151 2012-05-24 08:40
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)