我使用承诺函数promisify XHR,我想知道如何获得响应,并将其发回服务器,如果响应成功。使用承诺构造函数发送一个值到服务器
我做这样的事情
function createChannel(method, url) {
return new Promise(function (resolve, reject) {
xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function() {
if (xhr.readyState == 4) {
var hashValue = resolve(JSON.parse(xhr.responseText));
console.log(hashValue);
}
else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function() {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send(json);
});
}
createChannel(method, url)
.then(function (datums) {
console.log(datums)
}).catch(function (err) {
console.error('Sorry There Was An Error!', err.statusText);
});
如果这createChannel成功,我会想采取散列值的变量,并且向服务器请求获得新的价值。
.then(function (createChannel) {
console.log(createChannel);
});
这可能使用诺言吗? 谢谢你的建议。
目前还不清楚什么是“再发送出去”的意思?当你得到解决的承诺,并在'.then()'处理程序中获得'datums'值时,你想在那一点做什么? – jfriend00
hey @ jfriend00对于我所选择的措辞感到抱歉,我希望在获得解决的承诺时向服务器/网址发出新请求。 我想采取哈希变量,并提出新的发布请求 – PythonRookie
好吧,我在我的答案中显示。 – jfriend00