Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript XMLhttpresponse返回空数组_Javascript_Ajax_Xmlhttprequest - Fatal编程技术网

Javascript XMLhttpresponse返回空数组

Javascript XMLhttpresponse返回空数组,javascript,ajax,xmlhttprequest,Javascript,Ajax,Xmlhttprequest,我有一个简单的js代码: ajax.open("POST", "test.php", true); // //ajax.setRequestHeader(contentType, mimeType); // SETUP HEADER //ajax.setRequestHeader('Content-Type', 'text/plain'); ajax.onreadystatechange = function() { if (ajax.readyState == 4) { if(th

我有一个简单的js代码:

ajax.open("POST", "test.php", true); //
//ajax.setRequestHeader(contentType, mimeType); // SETUP HEADER
//ajax.setRequestHeader('Content-Type', 'text/plain');
ajax.onreadystatechange = function() {
  if (ajax.readyState == 4) {
    if(this.status == 200){
      console.log(ajax.response);
    }
  }
}
ajax.send("test");
字符串“test”应发送到test.php文件

test.php文件包含以下内容:

<?php
print_r($_POST);
?>

console.log(ajax.response)
返回一个空数组


为什么会这样?我是否必须执行其他操作才能从php文件中获得$\u POST打印?

如果您看到一个看起来像空数组的字符串(与空白相反),那么这是php的错误。编辑:尝试
ajax.send(“hello=world”)
,另外,您可能应该使用
echo-json\u-encode($var)而不是
print\r
。您没有向服务器发送值。你不应该使用echo吗?@epascarello为什么?我确实编写了ajax.send(“test”);和echo($\u POST)无效,因为这将是数组到字符串的转换