【发布时间】:2017-04-24 09:02:22
【问题描述】:
我正在尝试使用 XMLHttpRequest 调用 API,然后在成功状态下,我尝试将其重定向到下一页,但重定向正在运行,并且调用不起作用,如果我删除重定向,调用将起作用..
我不确定在调用 API 时是否无法重定向,或者我做错了什么,所以请检查并建议
这是我写的AJAX调用成功的代码。
if (response.status === true) {
var api = "https://api-voice.solutionsinfini.com/v1/?api_key=****&method=dial.click2call&output=xml&caller=****&receiver=" + sessionStorage.fullNumber;
// Calling the API
var xhttp = new XMLHttpRequest();
xhttp.open("GET", api);
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById('myModalCallback').style.display = 'none';
window.location.href = "/anotherpage";
}
};
xhttp.send();
document.getElementById('myModalCallback').style.display = 'none';
}
【问题讨论】:
-
你的重定向在你得到状态200时有效,所以你的api也有效,那么问题出在哪里?
-
问题是,通过将重定向放在 onreadystatechange 函数中,api 不起作用,如果我放在外面,则重定向不起作用.. api 应该生成一个电话,但那是不使用重定向
-
你读过这个问题吗? stackoverflow.com/questions/5183178/…
-
你能用jQuery代替吗?
标签: javascript ajax redirect xmlhttprequest