Chain
Chain

Reputation: 627

How to use the options of jQuery .load

I'm trying use jQuery's AJAX handling .load while passing several variables along with the request.

Normally, as a xmlhttp request GET, I would pass variables like so:

xmlhttp.open("GET","myfile.php?        
var1="+data1+"&var2="+data2+"&var3="+data3+"&var4="+data4,true);

Using load, I can't get this to work (the request doesn't succed):

$('#txtHint').load("myfile.php?var1="+data1+"&var2="+data2+"&var3="+data3+"&var4="+data4 , null, function (){ });

In the space where it says "null" in the .load example, is the parameter for: "data: This optional parameter represents a map of data that is sent with the request".

If I'm unserstanding that correctly, I believe I can use it to send my variables along with the request. However, I can't find information on how to format it.

How can I pass my variables along with the request using .load?

Upvotes: 0

Views: 39

Answers (1)

ajp15243
ajp15243

Reputation: 7960

It looks like your method should work as a GET to myfile.php.

If you want to use the data parameter instead, then look at the official jQuery API docs for the .load() method (not to be confused with the .load() event handling method), where it says:

data
Type: PlainObject or String
A plain object or string that is sent to the server with the request.

So you can pass it as a string or as an object.

As a string (which you more or less already have):

$('#txtHint').load('myfile.php',
    "var1="+data1+"&var2="+data2+"&var3="+data3+"&var4="+data4,
    function() { }
);

As an object (note that this makes your request a POST):

$('#txtHint').load('myfile.php',
    {
        var1: data1,
        var2: data2,
        var3: data3,
        var4: data4
    },
    function() { }
);

Most of the examples in the jQuery docs don't use the data parameter, but the last two examples at the very bottom of the page (as of typing this answer) shows some basic examples of using it in the object format.

Upvotes: 2

Related Questions