代码之家  ›  专栏  ›  技术社区  ›  simplyharsh

Jquery ajax错误回调

  •  34
  • simplyharsh  · 技术社区  · 15 年前

    我需要一些建议或者解释。我有一个jquery ajax调用,

    $.ajax({
     type: "GET",
     url: base_url+'/ajax/fetch/counts/',
     dataType: 'json',
     data: {},
     error: function(xhr, error){
            console.debug(xhr); console.debug(error);
     },
     success: display_counts
    });
    

    很好用。我的 success error 每次都会触发回调,即使我的调用返回成功状态200。在上面 xhr.status 是200。

    有人能解释出哪里出了问题,或者这里发生了什么吗? 错误 只有当我有404或者可能是非200响应时,回调才会触发。我的假设正确吗?

    谢谢。

    6 回复  |  直到 15 年前
        1
  •  31
  •   eis    13 年前

    对http错误调用错误回调,但

        2
  •  34
  •   palaѕн    13 年前

    $.ajaxSetup()

    $(function() {
        $.ajaxSetup({
            error: function(jqXHR, exception) {
                if (jqXHR.status === 0) {
                    alert('Not connect.\n Verify Network.');
                } else if (jqXHR.status == 404) {
                    alert('Requested page not found. [404]');
                } else if (jqXHR.status == 500) {
                    alert('Internal Server Error [500].');
                } else if (exception === 'parsererror') {
                    alert('Requested JSON parse failed.');
                } else if (exception === 'timeout') {
                    alert('Time out error.');
                } else if (exception === 'abort') {
                    alert('Ajax request aborted.');
                } else {
                    alert('Uncaught Error.\n' + jqXHR.responseText);
                }
            }
        });
    });
    
        3
  •  2
  •   Chris Laplante    15 年前

    1. 确保已通过设置禁用缓存 cache: false .
    2. 如果您使用的是Firefox,请尝试使用Firebug和Net选项卡来监视请求
    3. 不要依赖浏览器的JSON解析器。我推荐这个: https://github.com/douglascrockford/JSON-js/blob/master/json2.js 来自JSON的创建者
        4
  •  1
  •   Community Mohan Dere    9 年前

    recent question 有类似的问题 json jquery请求,尝试删除 () 从你的json响应中。

        5
  •  1
  •   Josh Andreas Rehm    15 年前

    success 处理程序导致错误。jQuery中也是这样吗?您可以通过将 display_counts 在一个 try..catch block

        6
  •  -1
  •   Kevin Kopf    9 年前

    改变 dataType plain/text html