使用jQuery处理http错误的问题AJAX web服务调用
问题描述:
我正在开发一个jQuery应用程序,在那里我需要捕获HTTP错误以及何时发生。以下是我的片段。使用jQuery处理http错误的问题AJAX web服务调用
// 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;
}
但是,当我编程修改,并在调用HTML页面中的web服务的URL持有一个错误的值,我能够在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');
});
感谢ManseUK。我已经删除了我的handleError代码..并且添加了一个更多的状态代码到你的代码中,如:504:function(){alert('Unknown host'); }。我可以在调用我的html的时候在萤火虫中看到这样的错误,但是在我的代码中不可捕捉。有什么理由? – 2012-03-19 16:40:19
@DavidR它没有做任何事!?!?!什么叫它? – ManseUK 2012-03-19 16:41:24
谢谢ManseUK。我已经删除了我的handleError代码..并且添加了一个更多的状态代码到你的代码中,如:504:function(){alert('Unknown host'); }。我可以在调用我的html的时候在萤火虫中看到这样的错误,但是在我的代码中不可捕捉。有什么理由? – 2012-03-19 16:43:52