1

I have this jquery autocomplete code.Everything works fine data is loaded etc.Success is working.But when i have error...error handling is not working in below code.

 $("#autocomplete").on("filterablebeforefilter", function (e, data) {

      if (value && value.length > 0) {
        //$ul.listview("refresh");
             $('.ui-responsive-panel').enhanceWithin();
             $.ajax({
                       type: "GET",
                       url: "http://domain.com/food.php",
                       dataType: "jsonp",
                       crossDomain: true,
                       data: $(this).serialize(),
                       success: function (data) {
                            alert("success");
                       },
                       error: function () {
                             alert("an error occurred!");
                       },
                       beforeSend: function () {
                            // This callback function will trigger before data is sent
                       },
                       complete: function () {
                            setTimeout(function () {}, 1);
                       }
                     })
                     .then(function (response) {
                             $.each(response, function (i, val) {
                                 //do something with data
                             });
                         }
                 });
chridam
  • 100,957
  • 23
  • 236
  • 235
Vishnu
  • 2,372
  • 6
  • 36
  • 58

2 Answers2

1

As the jQuery doc states for jQuery.ajax error handler functions:

Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

It is related to the technique of JSONP where the actual request is injected as a <script> tag. So a standard jqXHR object including an ajax error event isn't available. There's some plugin as workaround available. This plugin and solutions for dealing with network timeouts are discussed e.g. in this and this stackoverflow questions.

Community
  • 1
  • 1
matthias
  • 2,255
  • 1
  • 23
  • 28
  • Just same domain or also switching dataType? (=> jsonp is the problem!). – matthias Jun 19 '14 at 10:18
  • so what should be there instead of jsonp ? for same domain ? – Vishnu Jun 19 '14 at 10:22
  • "json" for example? But you probably have reasons for choosing "jsonp"!(?) Please first understand what the difference between json and jsonp is - then re-read my answer. It would exceed the comments' area purpose discussing this here. – matthias Jun 19 '14 at 10:28
  • yea thanks i read it..converting jsonp to json is working fine now :) – Vishnu Jun 19 '14 at 10:29
  • Happy to hear that :). Well, if you nevertheless need cross-domain resource access with jsonp, you should have a look on the other answers i referred to. – matthias Jun 19 '14 at 10:32
0

Try handling the error in the then (or use done() and fail() )

  $.ajax({
           //... code omitted ...
                 })
                 .then(
                     function (response) {
                         $.each(response, function (i, val) {
                             //do something with data
                         });
                     },
                     function (error) {
                         //do something with error
                     }
             });
Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101