当我使用JS打电话时,从Google API获得2条回复

时间:2019-06-20 14:19:08

标签: javascript xmlhttprequest

我正在使用Google API和javascript,当我打电话时,我从API得到2条响应。有没有一种方法可以将响应限制为首次拨打电话?

const Http = new XMLHttpRequest();
const url = "API_LINK_TO_CALL"
;

Http.open("GET", url);
Http.send();                                                                                                                                                                                                                      

Http.onreadystatechange=(e)=>{
    result = JSON.parse(Http.responseText);
    console.log(result);
}

1 个答案:

答案 0 :(得分:0)

等待响应准备就绪

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState

Http.onreadystatechange = function() {
    if (Http.readyState == XMLHttpRequest.DONE) {
        const result = JSON.parse(Http.responseText);
        console.log(result);
    }
}