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