我仍然在学习JavaScript的基础知识,我正在尝试制作一个简单的GET Http请求,以从API返回信息,但是responseText不会返回。下面是代码:
var xhr = new XMLHttpRequest();
xhr.open('GET', "https://api.apithis.net/dictionary.php?define=hi", true);
xhr.send();
console.log(xhr.responseText)
发布于 2017-04-10 12:44:11
这是因为你稍后才会得到回应。所以你需要处理异步。要做到这一点,您需要在回调函数中处理响应,这将在您得到响应时触发。
我建议你至少使用JQuery --这在一开始就有帮助。https://api.jquery.com/jquery.get/
如果您仍然使用xhr (在xhr.send之前),我认为它可以使用:
xhr.onreadystatechange = function () { console.log(this.responseText) }https://stackoverflow.com/questions/43323342
复制相似问题