Javascript XMLhttpresponse返回空数组
我有一个简单的js代码: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
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)如果您想使用JS中的数据,请使用code>而不是print\r
。您没有向服务器发送值。你不应该使用echo吗?@epascarello为什么?我确实编写了ajax.send(“test”);和echo($\u POST)无效,因为这将是数组到字符串的转换