gpt4 book ai didi

php - jQuery $.post 发送正确的变量,但 PHP 什么也没收到

转载 作者:搜寻专家 更新时间:2023-10-31 21:41:20 25 4
gpt4 key购买 nike

我有这个 jQuery 脚本,它将用户名和密码发送到 PHP 文件,该文件检查该用户是否存在并返回一个 JSON 用户对象。在非 IE 浏览器中工作正常,但 IE 失败。

奇怪的是,IE“firebug”说一切正常,但 PHP 脚本没有收到任何变量...

这是请求正文:

username=johanderowan&password=1234

这些是请求 header (出于安全原因,我省略了一些变量):

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

响应正文是(前三个空数组是 $_GET、$_POST 和 $_REQUEST):

Array ( )
Array ( )
Array ( )
{"status":"error","message":"No username or password specified.","httpCode":500}

这是请求脚本:

$('.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;
});

我完全不知道这里可能出了什么问题......

最佳答案

IE 似乎没有在跨域请求中发送正确的内容类型。内容类型始终设置为“文本/纯文本”。

阅读此博文中有关此缺点的更多信息:http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

我们通过解析 php://输入字符串并将其设置为 $_POST 变量来在服务器上修复此问题。

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;
}
}

关于php - jQuery $.post 发送正确的变量,但 PHP 什么也没收到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10721428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com