开发者

What would be the jQuery equivalent of this Prototype Ajax invocation?

In Prototype, this Ajax call posts its form to the server as a URL encoded string of name-value pairs, 开发者_StackOverflow中文版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?


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
    }
  });
}


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜