Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can jQuery .load fail?

I am using a simple ajax loader to get content on wordpress.

$("#page_preview").load("ajaxloader/", function(response, status, xhr) {
      if (status == "error") {
        alert("Sorry but there was an error");
      }
      else
      {
          $('#page_preview').fadeIn(300);
      }
    });
return;

When I load a specific post that has a google map embedded, obviously something goes wrong BUT instead of going inside the if statement, the firebug shows that it goes beyond this code. Neither if or else hit.

Using the eclipse debugger I found that the page load successfully, but when it returns the data to the .load() method, the last breaks.

Any ideas on what might going on?

like image 985
Odys Avatar asked Sep 15 '11 15:09

Odys


People also ask

What is AJAX failure?

The AJAX fail is a global event that triggered on the document to call handler function, which may be listening. The ajax fail can be performed with the help of the ajaxError() function. The jQuery ajaxError() function is a built-in function in jQuery.

Is jQuery load asynchronous?

You can use jQuery to support both synchronous and asynchronous code, with the `$.

Is jQuery load deprecated?

The load() method was deprecated in jQuery version 1.8 and removed in version 3.0. Use the on() or trigger() method instead.

Can jQuery be loaded from an external site?

Is it possible to load a single page from an external website? Yes, it's possible, but you'll need 1 line of PHP :) If you only need RSS feeds and you don't mind relying on Google you could use jquery-feeds.


1 Answers

How

<script>
    // as of jQuery 1.5.x ++
    var pagePreview = $("#page_preview");
    $.get("ajaxloader/").success(
        function(response, status, jqXhr) {
            alert("Success!");
            pagePreview.empty();
            pagePreview.append(response);
            // i feel you need to parse the response for the google map div, and perform another $.get() of the google url?
        }).error(function(response, status, jqXhr) {
        alert("These are not the droids you are looking for. move along.");
    }).complete(function(response, status, jqXhr) {
        alert("Complete!");
    });
</script>

#Why

jQuery.load() will get you on the documentation. .load is equivalent to this

It is roughly equivalent to $.get(url, data, success) except that it is a method rather than global function and it has an implicit callback function. When a successful response is detected (i.e. when textStatus is "success" or "notmodified"), .load() sets the HTML contents of the matched element to the returned data.

You need to register in $.ajax( complete:function(responseText, textStatus, XMLHttpRequest)){}); and check the textStatus value. if ok, then load the data into the destination $('selector') yourself. Or fix the .load() by watching your network xmlHttpRequests in chrome (ctrl + shift +j) or some network tab in firebug and debug the issue with your 'url' value in $('selector').load( 'url', function(response, status, xhr){})

like image 172
DefyGravity Avatar answered Oct 24 '22 11:10

DefyGravity