admin管理员组文章数量:1356337
I'm using the following code to send a request:
var ajaxHandler = new XMLHttpRequest();
ajaxHandler.onreadystatechange = function()
{
if(ajaxHandler.readyState == 4)
{
console.log(ajaxHandler.responseText);
}
}
ajaxHandler.open("POST", "filterCards", true);
ajaxHandler.send("category="+category+"&tag="+tag);
On the PHP side, I have this:
var_dump($_POST);
However, even though both the variables of category and tag have values, the console logs an empty array. What am I doing wrong with the post?
I'm using the following code to send a request:
var ajaxHandler = new XMLHttpRequest();
ajaxHandler.onreadystatechange = function()
{
if(ajaxHandler.readyState == 4)
{
console.log(ajaxHandler.responseText);
}
}
ajaxHandler.open("POST", "filterCards", true);
ajaxHandler.send("category="+category+"&tag="+tag);
On the PHP side, I have this:
var_dump($_POST);
However, even though both the variables of category and tag have values, the console logs an empty array. What am I doing wrong with the post?
Share Improve this question asked Apr 19, 2012 at 5:07 FibericonFibericon 5,80313 gold badges39 silver badges65 bronze badges2 Answers
Reset to default 6Add setRequestHeader
before your send call:
ajaxHandler.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxHandler.send("category="+category+"&tag="+tag);
Replace:
if(ajaxHandler.readyState == 4)
{
console.log(ajaxHandler.responseText);
}
with
if(ajaxHandler.readyState == 4 && ajaxHandler.status==200)
{
console.log(ajaxHandler.responseText);
}
Hope this helps.
The answer by web-nomad is correct, but if you are still receiving an empty array, recall whether you are hiding file extensions in your server configuration. If you are, then a request to "file.extension" will be redirected to "file", and the POST data is lost.
This is a small error to make, but also easy to overlook. It is easy to assume the error is in the code rather than in the URL, particularly as the array is returned at all - so one might think the URL is correct.
本文标签: phpXMLHttprequest sends an empty postStack Overflow
版权声明:本文标题:php - XMLHttprequest sends an empty post - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743974355a2570815.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论