我正在尝试使用jquery ajax
我们有一个网络服务,通常会停止维护,所以当我们打电话给webservice时,我们想要显示特定的消息,即"服务在维护中停机"。现在要处理我们编写的以下代码
$.ajax({
url: objWSConfiguration.VERIFY_USER,
dataType: "json",
data: jsonData,
type: "POST",
contentType: "application/json; charset=utf-8",
async: false,
timeout: 1000,
statusCode: {
503: function() {
navigator.notification.alert(GENERAL_SERVER_DOWN_ERROR_MSG, null, "Hitachi", "OK");
}
},
success: function (responseData, textStatus, jqXHR)
{
// perform a success processing
console.log("WSVerifyUser :response data " + JSON.stringify(responseData));
//Check Response Status
if(textStatus == "success")
{
if(responseData.status == STATUS_OK){
callback(responseData);
}else if(responseData.status == STATUS_ERROR){
callback(responseData.errorMessage);
}
} else {
navigator.notification.alert("Connection Error, Please try again later", null, "Hitachi", "OK");
}
},
error: function (jqXHR, textStatus, errorThrown)
{
console.log("Error 1 "+textStatus);
console.log("Error 2 "+errorThrown);
console.log(jqXHR.status);
if(textStatus === "timeout") {
console.log("Fall here timeout ");
//do something on timeout
navigator.notification.alert("Request Timed out...", null, "","OK");
} else {
// show error to the user about the failure to invoke the service
console.log("Fall here no where");
navigator.notification.alert(CONNECTION_ERROR_MSG, null,"", "OK");
}
}
});
我们尝试通过添加状态代码503
的回调来处理它,但问题是当我们的服务停止时它永远不会被调用。
它总是属于console.log("Fall here no where");
,而textStatus也没有在timeout
函数中返回error
但是超时和服务不可用是两回事,所以如何使用ajax
来处理它