2010-09-05 78 views
2

在原型,这个Ajax调用职位的形式向服务器名称 - 值对的URL编码字符串,因为你会发现在一个HTTP GET请求:这个Prototype Ajax调用的jQuery等价物是什么?

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

您会如何做同样的事情jQuery的?

回答

4

由于默认methodAjax.Request是POST,等效$.post()呼叫是这样的:

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

如果您不需要/不关心响应,这将做到:

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

或者,如果你是专门提取的脚本,然后它会看起来像这样,使用$.ajax()

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

序列化的jQuery VS的原型是如果没有参数只相当于,但在原型时,是'true',函数返回一个对象而不是一个字符串。由于OP需要'.serialize(true)'的结果,因此jquery的'.serialize()'版本不会产生相同的结果。见[这里](http://stackoverflow.com/questions/3414271/is-there-any-equivalent-in-jquery-for-prototype-serialize)和[这里](http://api.prototypejs.org/ dom/Form/serialize /)了解详情。然而,我不知道,如果请求通过分配字符串而不是对象来工作。 – DiegoDD 2013-10-03 18:05:29

0

使用get() Ajax请求,并serialize -ing形式:

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