약속 함수를 사용하여 XHR을 약속하고 있습니다. 응답을 얻는 방법을 알고 싶습니다. 그리고 응답이 성공하면 서버에 다시 게시하십시오. 이 성공 createChannel 경우약속 생성자를 사용하여 서버에 값을 다시 보내십시오.
나는 내가 hashvalue 변수를 가지고, 새로운 가치를 얻을 수있는 서버에 요청을 같은 것,이
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);
});
같은 일을하고있다.
.then(function (createChannel) {
console.log(createChannel);
});
약속을 사용하면 가능합니까? 조언 해 주셔서 감사합니다. 당신의 .then()
핸들러 내부
그것은 분명 어떤 수단 "을 다시 보내"? 해결 된 약속을 얻고'.then() '핸들러에'datums' 값을 얻으면, 그 시점에서 당신은 무엇을하고 싶습니까? – jfriend00
안녕하세요 @ jfriend00 wordings 내 선택에 대해 미안 해요, 내가 서버/URL에 내가 새로운 약속을 얻을 때 새로운 요청을하고 싶습니다. 해시 변수를 가져 와서 새로운 게시물 요청을 작성하려고합니다. – PythonRookie
OK, 그 대답을 보여주었습니다. – jfriend00