AJAX请求提交,但不接收POST值
AJAX request submits, but doesn't recieve POST values
我正在做一些事情,我有一个非常奇怪的问题。我提交一个AJAX请求,如下所示:
x = new XMLHttpRequest();
x.open('POST', url, true);
x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
x.onload = function(){
console.log(x.responseText);
};
x.send(data);
问题是,当我提交请求时,PHP没有接收到POST
值。data
变量如下所示:
Object { wordtype: "noun", word: "computer" }
和PHP如下:
if(!isset($_POST['wordtype']) || !isset($_POST['word'])){
echo "error 1";
exit;
} else {
$wordlist = json_decode(file_get_contents("words.json"), true);
$wordlist[$_POST['word']] = $_POST['wordtype'];
file_put_contents("words.json", json_encode($wordlist));
echo "success";
}
x.responseText
的值始终为error 1
;
谢谢
雅克Marais说
下面的例子:
var http = new XMLHttpRequest();
var url = "ajax.php";
var params = "wordtype=noun&word=computer";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
相关文章:
- 当我在form_for中的text_field_tag中点击Enter时,如何禁止发出POST请求
- 为什么 AJAX POST 请求不起作用
- Ajax POST请求没有'我不了解PHP
- 防止双击执行两次jQuery post请求
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 如何使用Javascript获取POST请求填充的元素的值
- NodeJS中POST请求的请求体为null
- 在Fiddler JavaScript中识别POST请求
- 使用application/x-www-form-urlencoded使用node.js在post请求中发送数组
- 打开一个选项卡,并在firefox网络扩展中向其发出POST请求
- 通过AJAX向同一页面发送POST请求,并使用$_POST获取值
- Ajax Post请求不起作用
- 如何在cloud9中向server.js发送post请求
- mootools表单json post请求
- 可以'无法在CasperJS中复制POST请求
- 使用用户名/API令牌javascript的Jenkins POST请求
- 在表单上注入javascript以发送post请求
- 使用javascript在post请求中传递用户输入变量
- 如何使用javascript进行post请求
- 如何在Javascript中通过POST请求发送XML数据