Samantha J T Star
Samantha J T Star

Reputation: 32758

How can I convert from .load to .ajax?

I have the following code example that calls .load() with the following syntax:

.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] )

Is there a simple way I can convert my call so I am using .ajax with this syntax:

.ajax( url [, settings] )

Upvotes: 3

Views: 400

Answers (2)

Pragnesh Chauhan
Pragnesh Chauhan

Reputation: 8476

You can use following code instead of .load method

$.ajax({
type: 'GET',//method GET/POST
url: "url",//Your url here
data: {data},//data to be send
complete: function(jqXHR, textStatus){
 console.log(jqXHR);
 console.log(textStatus);
 }
});

Upvotes: 1

Joseph
Joseph

Reputation: 119837

.load() is simply .get() that directly appends it's contents to the attached element.

Therefore, the .ajax() version should be one that has a GET for it's method, or in jQuery's case, type:

$.ajax({
    type: "GET",
    url: "someURL.php",
    data: {...key-value pair parameters...},
    dataType : 'html'
}).done(function(msg) {
    $('html_selector').html(msg);
});

.ajax() return deferred objects, which explains the .done() in this code example.

Upvotes: 7

Related Questions