Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery $.ajax调用在请求完成之前浏览到另一个页面时引发错误_Jquery_Ajax - Fatal编程技术网

Jquery $.ajax调用在请求完成之前浏览到另一个页面时引发错误

Jquery $.ajax调用在请求完成之前浏览到另一个页面时引发错误,jquery,ajax,Jquery,Ajax,我在Firefox中遇到了一个问题(IE中没有),我的Javascript启动了对服务的调用,这可能需要一点时间来加载。如果用户在调用完成之前导航到另一个页面,则调用jQuery.ajax()对象的error方法 我正在寻找一种方法: 当用户在请求未完成时浏览到另一页时不会引发错误,或者 让错误方法区分“真实”错误和此错误 毫不奇怪,我的error方法向用户显示了一个错误,当错误是由导航到另一个页面引起时,我不想这样做 我的简单测试代码包括以下Javascript和一个休眠20秒然后返回字符串的

我在Firefox中遇到了一个问题(IE中没有),我的Javascript启动了对服务的调用,这可能需要一点时间来加载。如果用户在调用完成之前导航到另一个页面,则调用
jQuery.ajax()
对象的
error
方法

我正在寻找一种方法:

  • 当用户在请求未完成时浏览到另一页时不会引发错误,或者
  • 让错误方法区分“真实”错误和此错误
  • 毫不奇怪,我的error方法向用户显示了一个错误,当错误是由导航到另一个页面引起时,我不想这样做

    我的简单测试代码包括以下Javascript和一个休眠20秒然后返回字符串的服务

    <script type="text/javascript" language="javascript">
        $(document).ready(function ()
        {
            $('#testLink').click(function ()
            {
                $.ajax({
                    type: "POST",
                    url: "/Services/NU.Web.NUnet.Services.UserService.asmx/LongRunningService",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data, textStatus, xhr)
                    {
                        console.log(data);
                        console.log(textStatus);
                        console.log(xhr);
                    },
                    error: function (xhr, textStatus, errorThrown)
                    {
                        console.log(xhr);
                        console.log(textStatus);
                        console.log(errorThrown);
                    }
                });
            });
        });
    </script> 
    
    
    $(文档).ready(函数()
    {
    $('#testLink')。单击(函数()
    {
    $.ajax({
    类型:“POST”,
    url:“/Services/NU.Web.NUnet.Services.UserService.asmx/LongRunningService”,
    数据:“{}”,
    contentType:“应用程序/json;字符集=utf-8”,
    数据类型:“json”,
    成功:函数(数据、文本状态、xhr)
    {
    控制台日志(数据);
    console.log(textStatus);
    console.log(xhr);
    },
    错误:函数(xhr、textStatus、errorshown)
    {
    console.log(xhr);
    console.log(textStatus);
    console.log(错误抛出);
    }
    });
    });
    });
    
    试试:

    
    $(函数(){
    $('#testLink')。单击(函数(){
    $.ajax({
    类型:“POST”,
    url:“/Services/NU.Web.NUnet.Services.UserService.asmx/LongRunningService”,
    contentType:“应用程序/json;字符集=utf-8”,
    数据类型:“json”,
    成功:函数(数据、文本状态、xhr){
    控制台日志(数据);
    console.log(textStatus);
    console.log(xhr);
    },
    错误:函数(xhr、textStatus、errorshown){
    如果(xhr.status==500){
    console.log(xhr);
    console.log(textStatus);
    console.log(错误抛出);
    }
    }
    });
    });
    });
    
    太棒了,非常感谢!当浏览到另一个页面时,状态为0,因此我将对照该值进行检查。这是一个棘手的问题,因为我在Firebug中看不到任何东西,因为页面正在重新加载:xYour welcome。对于这些类型的错误,您可以使用Firebug控制台中的“持久化”按钮。
    <script type="text/javascript" language="javascript">
        $(function() {
            $('#testLink').click(function() {
                $.ajax({
                    type: "POST",
                    url: "/Services/NU.Web.NUnet.Services.UserService.asmx/LongRunningService",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data, textStatus, xhr){
                        console.log(data);
                        console.log(textStatus);
                        console.log(xhr);
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        if (xhr.status === 500) {
                            console.log(xhr);
                            console.log(textStatus);
                            console.log(errorThrown);
                        }
                    }
                });
            });
        });
    </script>