即使我使用 XMLHttpRequest 在 JavaScript 中调用了一次,为什么 api 响应两次? [英] Why does the api responds twice even though I have called once in JavaScript using XMLHttpRequest?
本文介绍了即使我使用 XMLHttpRequest 在 JavaScript 中调用了一次,为什么 api 响应两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我发送请求时,第一个响应为空,然后它显示两次 JSON 响应.我该如何解决这个问题?任何建议都会有所帮助.谢谢:(
When I send the request then the first response is empty then it shows the JSON response twice. How can I solve this? Any advice will be helpful. Thanks :(
代码:
function go() {
var cat;
var api = new XMLHttpRequest();
var ip = document.getElementById(
"ipBox").value;
var finalUrl =
"http://ipwhois.app/json/" + ip;
api.open("GET", finalUrl);
api.send();
api.onreadystatechange = (e) => {
cat = api.responseText;
console.log(cat)
};
}
推荐答案
readyState 有多个可能的值,包括loading";和交互式",而不仅仅是完整".改用 onload
.
readyState has multiple possible values, including "loading" and "interactive", not just "complete". Use onload
instead.
这篇关于即使我使用 XMLHttpRequest 在 JavaScript 中调用了一次,为什么 api 响应两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文