从 XHR 请求返回一个值

问题描述

考虑以下代码

function checkValidResultId (resultId) {
  const endpoint = `http://localhost:3000/questiontree/checkValidResultId/${resultId}`;
  
        const xhr = new XMLHttpRequest ();
        xhr.responseType = 'json';
        xhr.onreadystatechange = () => {
          if (xhr.readyState === XMLHttpRequest.DONE) {
            console.log(xhr.response[0]);
            return xhr.response[0];
    }
        };
        xhr.open('GET',endpoint);
        xhr.send('');  
};
console.log(checkValidResultId("6108895"));

为什么 console.log(xhr.response[0]) 返回一个 Object,但 return xhr.response[0] 不起作用并返回 undefined ?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)