2010-09-05 77 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 
    } 
}) 
相關問題