我们如何在错误的资源请求的情况下使用 XMLHttpRequest 函数处理程序
How we can use XMLHttpRequest function handler in case of wrong resource request?
在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/某些是各种不同的内容)。
相关文章:
- XMLHttpRequest未返回值-状态202
- XMLHttpRequest在移动设备上的chrome上不起作用
- AJAX简单错误.XMLHttpRequest无法加载http://localhost/mpl/getPage.php.
- 检查xmlhttprequest问题的消息
- 主线程上的同步XMLHttpRequest已弃用
- 如何定期发出xmlhttprequest
- javascript第三个XMLHttpRequest被拒绝.但前两个是允许的
- 上传带有XMLHttprequest的文件-多部分/表单数据中缺少边界
- 对象#<XMLHttpRequest>没有方法'完成'
- 在XMLHttpRequest之后重新初始化jQuery
- 如何将GreaseMonkey中的XMLHttpRequest延迟到目标页面加载完成
- Javascript XMLHttpRequest——只有第一个POST请求有效
- XMLHttpRequest - difference between Chrome & Firefox
- IE上的新XMLHttpRequest()出现JS Ajax未指定错误
- 非常简单的XMLHttpRequest不起作用
- 如何使用XMLHttpRequest下载文件
- XMLHttpRequest在$.ajax工作的地方给出了CORS错误
- XMLHttpRequest.open()AJAX中的参数url
- XMLHTTPRequest脚本中没有internet连接和超时
- 为什么当async标志设置为false时,xmlhttprequest中的代码可以工作,而当它设置为true时却不能工作