【发布时间】:2020-02-18 21:21:42
【问题描述】:
你能帮我处理这段代码 sn-p 吗?在这一部分中,我向我的 c# 控制器发出 ajax 请求,只是为了验证电子邮件并获取布尔值,但在此过程中,promise 返回状态pending 产生问题,因为我真的只需要promise 中的值,这里是我的代码:
let Email = input;
let valida = ValidaEmail(Email);
console.log(valida);
函数
async function ValidaEmail(email) {
let promise = new Promise(resolve => {
var string = JSON.stringify(email);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/Home/EmailisValid", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.onreadystatechange = () => {
if (xhttp.readyState === 4 && xhttp.status === 200)
resolve(xhttp.response);
};
xhttp.send(string);
});
let retorno = await promise;
console.log(retorno);
}
输出:
【问题讨论】:
-
你没有解决promise,你应该解决`xhttp.send(string);`response
-
像这样 resolve(xhttp.send(string)) ?
标签: javascript ajax promise