PDA

View Full Version : jQuery: Posting Data in the load functions


codegreen
02-14-2009, 01:53 AM
Hi,
I'm using the jQuery load function to make an AJAX call. I know that paired key : value variables placed after the url are POSTED but I need to be able to pass these values in dynamically depending on the function that's calling load.

so for example I need:

var post_content;

....

[post_content is set dynamically]

$('page_content').load(url, post_content, function() {..do stuff...});


In this case post_content is a global var whose value changes depending on the data I need to pass to the load function.

I've tried doing stuff like this:

post_content = "{'id' : '1', 'answer' : 'yes'}";

but instead of posting that data...it just adds it to the query string and does a GET.

Is there any way to dynamically set this post_content variable and have is passed so that it will POST as if I've done this:

$('page_content').load(url, {'id':'1', 'answer':'yes'}, function() {..do stuff...});

Because in that case the data gets POSTED.

Thank you in advance.

Iszak
02-14-2009, 10:17 AM
Why not just use the post function instead of the load, and that will ensure it'll be a POST. Also on your code you're setting post_content as a string and not as JSON? Because it's in speech marks.. and not simply in {} so maybe remove that and see if that makes any difference before switching to the post function.

codegreen
02-15-2009, 06:13 AM
Thanks...that's exactly what I needed!