XMLHtt prequest发送一个空职位 [英] XMLHttprequest sends an empty post

查看:96
本文介绍了XMLHtt prequest发送一个空职位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code发送一个请求:

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

在PHP的一面,我有这样的:

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?

推荐答案

添加 setRequestHeader 您发送调用之前:

ajaxHandler.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxHandler.send("category="+category+"&tag="+tag);

替换:

   if(ajaxHandler.readyState == 4)
   {
      console.log(ajaxHandler.responseText);
   }

   if(ajaxHandler.readyState == 4 && ajaxHandler.status==200)
   {
      console.log(ajaxHandler.responseText);
   }

希望这有助于。

Hope this helps.

这篇关于XMLHtt prequest发送一个空职位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆