After doing an AJAX call, am redirecting- using the following code. But, after AJAX completion, it gives Error alert; then its redirecting.
callService($serviceUrl, $serviceName, 'POST');
//redirecting to nextpage
window.location.href = "nextPage.html";
function callService($serviceUrl, $serviceName, $method) {
......
$.ajax({
url : $serviceUrl,
dataType : "json",
type : $method,
data : $requestData,
success : function (data) {
switch($serviceName) {
case 'getTopRated': populateTopRated(data);
break;
case 'GetLatestScoop': populateLiveScoop(data);
break;
.....
}
$.mobile.pageLoading(true);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("Error");
$.mobile.pageLoading(true);
}
});
}
Is there a way to resolve it without- modifying the function?
Wait until the
ajaxrequest complete before you redirect to another page. Since you redirect as soon as calling theservicetherequestabortsand thenerrorhandler is triggered due to which you are seeing thealertmessage from error handler. Move the code to redirect insideajaxsuccesscallback it will work.