user545520
user545520

Reputation: 161

Jquery post with javascript parameters as data

I am new to jquery ,so i need some help to make a request to server with post.

Why i have chosen $.post is i have large ammount of data to pass to the server.

I have javascript variables like action="Next",resultData=""(this is the very large string). So how to pass this javascript varaiables to $.post?

Thanks.

Upvotes: 1

Views: 287

Answers (2)

Incognito
Incognito

Reputation: 20765

It's all defined in the man page for it $.post is just an alias of $.ajax. I would suggest using $.ajax instead just because you have more control and it's more maintainable in the long run.

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success
  dataType: dataType
});

You can either define data in it's var1=data1&var2=data2&var3=data3 string format, or as an object. I suggest using objects because it's eaiser to see and work with.

{
  "var1" : "data1",
  "var2" : "data2",
  "var3" : "data3"
}

Upvotes: 1

karim79
karim79

Reputation: 342635

Just use an object as $.post's optional second parameter, like this:

$.post("foo.html", { action: "Next", resultData: '...' }, function(html) {
    // success callback
});

Upvotes: 2

Related Questions