Jquery ajax error callback

后端 未结 6 1844
暖寄归人
暖寄归人 2020-11-28 09:08

I need some suggestions here or maybe some explanations. I have a jquery ajax call,

$.ajax({
 type: \"GET\",
 url: base_url+\'/ajax/fetch/counts/\',
 dataTyp         


        
相关标签:
6条回答
  • 2020-11-28 09:42

    Just an suggestion, try using the $.ajaxSetup() to get the correct error like this:

    $(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);
                }
            }
        });
    });
    
    0 讨论(0)
  • 2020-11-28 09:44

    A recent question had similar problem with json jquery requests, try removing surrounding () from your json response.

    0 讨论(0)
  • 2020-11-28 09:51

    I'm not a jQuery expert, but I know that bwith Prototype.js, the AJAX error handler fires if the request is successful but the success handler causes an an error. Is that the same in jQuery? You could test if this is what's happening by putting the entire contents of display_counts in a try..catch block.

    0 讨论(0)
  • 2020-11-28 09:54

    Error callback is called on http errors, but also if JSON parsing on the response fails. This is what's probably happening if response code is 200 but you still are thrown to error callback.

    0 讨论(0)
  • 2020-11-28 09:55

    Change dataType from plain/text to html

    0 讨论(0)
  • 2020-11-28 09:59

    A few things I can think of:

    1. Make sure you have disabled caching by setting cache: false.
    2. If you are using Firefox, try using Firebug and the Net tab to monitor the request
    3. Don't rely on the browser's JSON parser. I would recommend this one: https://github.com/douglascrockford/JSON-js/blob/master/json2.js from the creator of JSON no less
    0 讨论(0)
提交回复
热议问题