如果资源请求错误,我们如何使用XMLHttpRequest函数处理程序?

时间:2022-08-01 19:41:16

In angular js when we provide the wrong url in $http.get('').then(onpass,onfail) method we just get the error message it would not show any error message in console.

在角度js中,当我们在$ http.get('')。然后(onpass,onfail)方法中提供错误的URL时,我们只是得到错误消息,它不会在控制台中显示任何错误消息。

example

$http.get('path').then(onpass, onerror);

var onpass = function(request){$scope.data = request.data };
var onerror = function(request){$scope.error= "error" };

Just wanted to know how we can use the same functionality XMLHttpRequest. It would be great if you provide me just one example. I use the same

只是想知道我们如何使用相同的功能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);

Thanks

1 个解决方案

#1


0  

You just check for other combinations of readyState and status (with 0/something generally being a security issue and 4/something being all sorts of different things).

您只需检查readyState和status的其他组合(0 / /通常是安全问题,4 /是各种不同的东西)。

#1


0  

You just check for other combinations of readyState and status (with 0/something generally being a security issue and 4/something being all sorts of different things).

您只需检查readyState和status的其他组合(0 / /通常是安全问题,4 /是各种不同的东西)。