views:

33

answers:

2

In Prototype, this Ajax call posts its form to the server as a URL encoded string of name-value pairs, as you would find in a HTTP GET request:

function doajax()
{
 var current_req = new Ajax.Request('/doajax', {
 asynchronous:true, 
 evalScripts:true,
 parameters: $('ajax_form').serialize(true)}
 );
}

How would you do the same thing using jQuery?

A: 

Using get() ajax request, and serialize-ing the form:

$.get({
  url: '/doajax', 
  data: $('#ajax_form').serialize(),
  success: function (data) {//success request handler
  }
})
aularon
+4  A: 

Since the default method for Ajax.Request is POST, the equivalent $.post() call would look like this:

function doajax()
{
  $.post('/doajax', $('#ajax_form').serialize(), function(respose) {
    //do something with response if needed
  });
}

If you don't need/don't care about the response, this will do:

function doajax()
{
  $.post('/doajax', $('#ajax_form').serialize());
}

Or, if you're specifically fetching a script, then it'll look like this, using $.ajax():

function doajax()
{
  $.ajax({
    url:'/doajax', 
    type: 'POST',
    data: $('#ajax_form').serialize(),
    dataType: 'script',
    success: function(respose) {
      //do something with response if needed
    }
  });
}
Nick Craver