在Jquery AJAX调用给出Error之后重定向

时间:2021-11-29 20:36:42

After doing an AJAX call, am redirecting- using the following code. But, after AJAX completion, it gives Error alert; then its redirecting.

在进行AJAX调用之后,使用以下代码重定向。但是,在AJAX完成后,它会提供错误警报;然后它重定向。

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?

有没有办法解决它而不修改功能?

1 个解决方案

#1


3  

Wait until the ajax request complete before you redirect to another page. Since you redirect as soon as calling the service the request aborts and then error handler is triggered due to which you are seeing the alert message from error handler. Move the code to redirect inside ajax success callback it will work.

等到ajax请求完成后再重定向到另一个页面。由于您在调用服务后重定向请求将中止,然后触发错误处理程序,因为您正在从错误处理程序中看到警报消息。移动代码以重定向ajax成功回调它将起作用。

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;
            .....


            //redirecting to nextpage
            window.location.href = "nextPage.html";
        }
        $.mobile.pageLoading(true);
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
        alert("Error");
        $.mobile.pageLoading(true);
    }
  });

}

#1


3  

Wait until the ajax request complete before you redirect to another page. Since you redirect as soon as calling the service the request aborts and then error handler is triggered due to which you are seeing the alert message from error handler. Move the code to redirect inside ajax success callback it will work.

等到ajax请求完成后再重定向到另一个页面。由于您在调用服务后重定向请求将中止,然后触发错误处理程序,因为您正在从错误处理程序中看到警报消息。移动代码以重定向ajax成功回调它将起作用。

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;
            .....


            //redirecting to nextpage
            window.location.href = "nextPage.html";
        }
        $.mobile.pageLoading(true);
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
        alert("Error");
        $.mobile.pageLoading(true);
    }
  });

}