[英]jQuery $.post sends vars correct, but PHP receives nothing
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. 我有这个jQuery脚本,它向PHP文件发送一个用户名密码,该文件检查该用户是否存在并返回一个JSON用户对象。 Works fine in non-IE browsers, but IE fails.
在非IE浏览器中正常工作,但IE失败。
The wierd thing is that the IE "firebug" says everything is fine, but the PHP script doesnt recieve any vars... 奇怪的是,IE“firebug”说一切都很好,但PHP脚本没有收到任何变量......
This is the request body: 这是请求正文:
username=johanderowan&password=1234
用户名= johanderowan&口令= 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
Request = POST /1.0/account/login.json HTTP / 1.1
Accept = /接受= /
Origin = [DEVURL]Origin = [DEVURL]
Accept-Language = nl-NLAccept-Language = nl-NL
UA-CPU = AMD64UA-CPU = AMD64
Accept-Encoding = gzip, deflateAccept-Encoding = gzip,deflate
User-Agent = Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)User-Agent = Mozilla / 5.0(兼容; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident / 5.0)
Host = [DEVURL]主持人= [DEVURL]
Content-Length = 66内容长度= 66
Connection Keep-Alive Cache-Control no-cache连接Keep-Alive Cache-Control no-cache
The response body is (first three empty arrays are $_GET, $_POST and $_REQUEST): 响应体是(前三个空数组是$ _GET,$ _POST和$ _REQUEST):
Array ( )
数组()
Array ( )数组()
Array ( )数组()
{"status":"error","message":"No username or password specified.","httpCode":500}{“status”:“error”,“message”:“未指定用户名或密码。”,“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. IE似乎没有在跨域请求中发送正确的conent类型。 The content-type is always set to "text/plain".
内容类型始终设置为“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 在此博文中了解有关此缺点的更多信息: 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. 我们通过解析php://输入字符串并将其设置为$ _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. 您是否尝试将缓存设置为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.
您试图调用.json文件。 You could try to make a call to a .php file instead.
您可以尝试调用.php文件。
Make sure you put this in your .php file: 确保将其放在.php文件中:
header("Content-Type: application/json");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.