How can jQuery.load crash?

I am using a simple ajax loader to get content in 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 entry with a built-in google map, it is obvious that something is going wrong, BUT instead of going to the if , firebug shows that it is beyond the scope of this code. Neither if nor else fall.

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

Any ideas on what could happen?

+6
source share
3 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 provide you the documentation..load is equivalent to this

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

You need to register with $.ajax( complete:function(responseText, textStatus, XMLHttpRequest)){}); and check the value of textStatus . if everything is ok, upload the data to destination $('selector') yourself. Or fix .load() by looking at the xmlHttpRequests network in chrome (ctrl + shift + j) or some network tab in firebug and debugging the problem with your "url" value in $('selector').load( 'url', function(response, status, xhr){})

+4
source

An easy way to debug is to view XHR requests with the firebug console tab. There may be a problem with the request URL and you may not be able to get any data, or maybe you will get an error message or something else. With firebug, you can easily see what the server returns.

Since .load only loads successfully, you can add

 console.debug("In .load function"); 

when begging for a function. Again, by checking with firebug, you can see if the .load callback is valid.

0
source

use the $ .ajax function for example

  $.ajax({ url: "test.html", context: document.body, success: function(){ //when Successfully executed }, error: function(){ //When Error Fires } }); 
0
source

Source: https://habr.com/ru/post/897397/


All Articles