ajax.remotecall,Rails 3 AJAX遠程表單回調

I'm upgrading from rails 2.3.8 to 3.0.0, so I need to replace the remote_form_for helper calls with form_for(@object, :remote=>true).

我正在從rails 2.3.8升級到3.0.0,因此需要將remote_form_for helper調用替換為form_for(@object,:remote=>true)。

I've been following along with Simone Carletti but I cant seem to get the ajax callbacks from rails.js to fire.

我一直跟隨Simone Carletti,但我似乎無法從rails得到ajax回調。js開火。

My generated HTML is:

我的生成的HTML是:

The javascript I'm testing with:

我正在測試的javascript:

jQuery(function($){

alert('document ready');

$("#formname")

.bind('ajax:loading', function() {alert("loading!");})

.bind('ajax:success', function(data, status, xhr) {alert("success!");})

.bind('ajax:failure', function(xhr, status, error) {alert("failure!");})

.bind('ajax:complete', function() {alert("complete!");});

});

The 'document ready' alert fires, and the ajax request is successfully executed (data is posted to the server), but none of the 'ajax:____' callbacks fire.

“文檔就緒”警報觸發,ajax請求被成功執行(數據被發布到服務器),但是沒有任何“ajax:____”回調觸發。

What am I doing wrong?

我做錯了什么?

(for what it's worth, the form itself is loaded via ajax)

(值得注意的是,表單本身是通過ajax加載的)

3 个解决方案

#1

20

Wow, that was a colossal waste of an evening.

哇,真是浪費了一個晚上。

Doing it the way I did, the default prototype rails.js won't linkup the callbacks.

按照我的方式來做,默認的原型rails。js不會連接回調。

After converting to the jQuery based rails.js (http://github.com/rails/jquery-ujs) callbacks are working fine.

在轉換到基於jQuery的rails之后。js (http://github.com/rails/jquery-ujs)回調工作正常。

#2

2

You can trigger the Prototype-based callbacks like so:

您可以觸發基於原型的回調,如下所示:

$('formname').observe('ajax:before', function(){

...

});

$('formname').observe('ajax:complete', function(request){

...

});

However, I don't think the request object in the ajax:complete callback contains the response. I'm used to jQuery and had hoped that accessing the response would be simple. Instead, it looks like switching to jQuery-Rails may still be easier, as I can't seem to find drop-in Prototype code to replace the :update functionality from Rails 2 remote helpers.

但是,我不認為ajax中的請求對象:完全回調包含響應。我已經習慣了jQuery,並希望訪問響應會很簡單。相反,切換到jQuery-Rails可能更容易,因為我似乎找不到替代來自Rails 2遠程助手的:update功能的原型代碼。

#3

0

If the accepted answer don't fixed your problem, check if you have embedded multiple jQuery libraries. In my case I had two different version of jQuery embedded and after removing one of them, everything worked fine.

如果所接受的答案沒有解決問題,請檢查是否嵌入了多個jQuery庫。在我的例子中,我有兩個不同版本的jQuery嵌入,在刪除其中一個之后,一切都很好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值