处理jQuery AJAX Web服务调用的http错误的问题
Issues with handling http errors with jQuery AJAX webservice calls
我正在开发一个jQuery应用程序,在该应用程序中,我需要在HTTP错误发生时捕获它。下面是我的片段。
// Function to validate URL
function validateURL(url)
{
var pattern = new RegExp();
pattern.compile("^[A-Za-z]+://[A-Za-z0-9-_]+''.[A-Za-z0-9-_%&'?'/.=]+$");
if (!pattern.test(url))
{
return false;
}
return true;
}
// Generic error handler for handling the webservice requests.
function initWebService(wstype, wsurl,jsonData)
{
// If the method parameter is not either "GET" or "POST" display an error message to the developer.
var msgValidateArgument;
var wsCallStatus;
var callbackData;
if ((arguments[0] != 'GET') && (arguments[0] != 'POST'))
{
//alert("Invalid");
//alert("You must provide a valid http method in your webservice call.");
msgValidateArgument = "You must provide a valid http method in your webservice call.";
return msgValidateArgument;
}
// Making sure whether the developer is passing the required number of parameters.
if(arguments.length < 3)
{
//alert("Some required arguments seems to be missing. Please check your webservice invocation.");
msgValidateArgument = "Some required arguments seems to be missing. Please check your webservice invocation.";
return msgValidateArgument;
}
if (!validateURL(arguments[1]))
{
msgValidateArgument = "You must provide a valid URL in your webservice call.";
return msgValidateArgument;
}
if(arguments[2] != ''){
var response=jQuery.parseJSON(arguments[2]);
if(typeof response =='object'){
//It is JSON
alert(response.toSource());
}
else{
msgValidateArgument = "The JSON data being passed is not in valid JSON format.";
return msgValidateArgument;
}
}
// Making the AJAX call with the parameters being passed. The error handler handles some of the possble http error codes as of now.
$.ajax({
type: arguments[0],
url: arguments[1],
data: arguments[2],
dataType: 'json',
async:false,
statusCode:{
404: function(){
alert('Page not found');
},
500: function(){
alert('Page not found');
},
504: function(){
alert('Unknown host');
}
},
success: function(data){
//alert('Data being returned from server: ' +data.toSource());
//alert('Data being returned from server: ' +data.toSource());
//alert(data);
callbackData = data;
}
});
return callbackData;
}
但是,当我用程序更改Web服务url以保存错误的值时,在调用html页面时,我可以在firebug控制台中看到一条错误消息,但我的代码片段似乎根本没有捕捉到错误。
例如,在调用GEONames API时,我在firebug的控制台中遇到一个声明"需要407授权"。但即使我在错误块中处理该状态代码,它也不会启动。。原因可能是什么?。
难道我们没有任何全面的解决方案来有效地处理这些HTTP错误吗?。
我认为您的代码有一些问题。。。首先,handleError
是如何调用的?因为你调用了一个名为handleError
的方法,但什么都不传递。。。我假设你使用.ajax()
你应该这样做:
$.ajax({
statusCode: {
404: function() {
alert('page not found');
},
500: function() {
alert('server error');
}
},
success : {
alert('it working');
},
complete : {
alert('im complete');
});
相关文章:
- 从 Ajax 获取由 ZombieJS 的异步点击事件触发的 Http 错误
- 如何在使用JavascriptCallbackBehavior时引发带有HTTP错误代码的WebFaultExcepti
- 如何处理所有 AJAX 调用的特定 HTTP 错误
- ionic http获取到本地文件会给出http错误代码0
- 在 HTTP 错误回调中访问“this”
- 使用 Sentry 的 Raven JS 收集所有 HTTP 错误
- $http错误回调不能与 httpProvider responseInterceptor 结合使用
- 如何在控制台中防止/捕获http错误输出
- AJAX POST 请求中的 HTTP 错误 400
- 茉莉花嘲笑角$http - 错误:描述不需要完成参数
- 角度 如何在登录页面上显示 http 错误消息
- HTTP错误浏览器弹出窗口
- 如何用“重定向”来重定向网页;http错误404”;自动切换到默认网页
- 处理jQuery AJAX Web服务调用的http错误的问题
- ASP.Net MVC:HTTP错误414.请求URL太长
- 如何在AngularJs中使用ngView加载视图内容时为http错误定义处理程序
- 当请求失败时,Jquery不处理HTTP错误
- HTTP错误307-Node.js(Express)、passport.js、mongoose.js和Android应用
- Angular:未处理http错误的默认处理程序
- 如何在Jquery POST中获得HTTP错误响应代码?