我创建了一个POST数据对象的标准函数
post = function(url, data){
// Return a new promise.
return new Promise(function(resolve, reject) {
// Do the usual XHR stuff
var req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "application/json");
req.onload = function() {
// This is called even on 404 etc
// so check the status
if (req.status == 200) {
// Resolve the promise with the response text
resolve(req.response);
}
else {
// Otherwise reject with the status text
// which will hopefully be a meaningful error
reject(Error(req.statusText));
}
};
// Handle network errors
req.onerror = function() {
reject(Error("Network Error"));
};
var jayson = JSON.stringify(data);
req.send(jayson);
});
};
当我使用data = {key:value,anotherKey:value}调用此函数时,jayson变量如下所示:
"{"key":value, "anotherKey":value}"
最后,数据没有到达服务器(请求确实如此)。我得到200状态,所以在服务器上一切都很好。
(是的,它需要是一个POST - 不,我不想使用jQuery)
我的功能出了什么问题,它没有发送我的数据?