XMLHtt prequest发送一个空职位职位、XMLHtt、prequest

2023-09-10 14:25:45 作者:七秒鐘De記憶

我用下面的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.