Toni Michel Caubet
Toni Michel Caubet

Reputation: 20173

jQuery. .load() won't give a response/status

var destino = '#myContainer';

var file = 'my-file.html'; //file exists in folder.
var seccion = 'myTarget';    //
var nivel   = 1;      // #myTarget_1 exists in document


$('.load').show();
console.log('before');
$(destino).load('/'+file+' #'+seccion+'_'+nivel, function(response, status, xhr){
    $('.load').hide();
    console.log(response);
    console.log(status);
}
console.log('done');

log output:

before
done

Why is it posible that this doesn't return anything? No error or content. Can I forze it somehow to get an error if there is one?

Upvotes: 2

Views: 528

Answers (2)

Dan Blows
Dan Blows

Reputation: 21184

Based on comments above, the element on which you are calling $.load() is dynamically named. Therefore, ensure that the element exists before doing the load. e.g.

if($(destino)) {
   $(destino).load();
} else {
   console.log(destino + ' does not exist');
}

The alternative is to do as @Armin said, and use $.get() and then $.appendTo() as a success callback.

Upvotes: 1

Armin
Armin

Reputation: 15968

Use .get() instead of .load(). As I have understood the .load() function, it is pasting the specified result directly to defined node. There is no callback function necessary.

Load-Example:

$('#result').load('ajax/test.html#whatever');

The content id #whatever on this page will be pasted to #result of the current page.


Get-Example:

var url = '/' + file;
$.get(url , function(data) {
    $('.load').hide();
    console.log($(data).find('#' + seccion + '_' + nivel));
});

Upvotes: 2

Related Questions