我们如何在错误的资源请求的情况下使用 XMLHttpRequest 函数处理程序

How we can use XMLHttpRequest function handler in case of wrong resource request?

本文关键字:XMLHttpRequest 函数 程序 处理 情况下 请求 错误 资源 我们      更新时间:2023-09-26

在angular js中,当我们在$http.get('').then(onpass,onfail)方法中提供错误的url时,我们只会收到错误消息,它不会在控制台中显示任何错误消息。

$http.get('path').then(onpass, onerror);
var onpass = function(request){$scope.data = request.data };
var onerror = function(request){$scope.error= "error" };

只是想知道我们如何使用相同的功能 XMLHttpRequest。如果你只给我举一个例子,那就太好了。我用的一样

xhttp = new XMLHttpRequest();
xhttp.open('POST', service + url, false);
xhttp.setRequestHeader("Accept", "application/json");
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.setRequestHeader("APIKey", apiKey);
xhttp.onreadystatechange = function () {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        var data = xhttp.responseText;
       // console.log(data);
    }
}
var payload = "{'xyz':{'abc':'" + test+ "', 'test':'" + test+ "'}}";
xhttp.send(payload);

谢谢

您只需检查 readyState 和状态的其他组合(0/某些通常是安全问题,4/某些是各种不同的内容)。