Vikas Vikas - 1 year ago 142
jQuery Question

How to use getJSON, sending data with post method?

I am using above method & it works well with one parameter in URL.


where controller/action/parameter format is applied.

Now I have an action in Students controller that accepts two parameters and return a JSON object.

So how do I post data with
using post method?

Similar methods are also acceptable.

The point is to call an action of the controller with AJAX.

Answer Source

The $.getJSON() method does an HTTP GET and not POST. You need to use $.post()

$.post(url, dataToBeSent, function(data, textStatus) {
  //data contains the JSON object
  //textStatus contains the status: success, error, etc
}, "json");

In that call, dataToBeSent could be anything you want, although if are sending the contents of a an html form, you can use the serialize method to create the data for the POST from your form.

var dataToBeSent = $("form").serialize();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download