"ajax:成功";被叫两次 [英] "ajax:success" being called twice

查看:257
本文介绍了"ajax:成功";被叫两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery_ujs发送一个简单的帖子请求.我将回调函数绑定到"ajax:success"事件.但是,此回调对于每个成功的帖子都会被调用两次.

I'm using jquery_ujs to send a simple post request. I bound a callback function to the "ajax:success" event. However, this callback is being called twice for each successful post.

我知道这类似于其他问题,但是我检查了我的源代码,它似乎没有两次加载jquery_ujs.js文件(许多响应都建议).

I know this is similar to this other question, but I checked my source and it doesn't appear to be loading the jquery_ujs.js file twice (as suggested by many of the responses).

    $(".new_item").bind("ajax:success", 
    function(xhr, data, status) 
    {
                 /* this is called twice for each call!*/
    }

有人有指针吗?谢谢!

推荐答案

正在发生一些事情.

要么您拥有.new_item类的多个元素

Either you have multiple elements with .new_item class

alert($(".new_item").length);

您两次发送了ajax请求

you are sending the ajax request twice

alert("Sending Request");
$.ajax(...

或者您将事件绑定两次

alert("Binding Event");
$(".new_item").bind("ajax:success", function(xhr, data, status) {

注意:如果您使用的是现代浏览器,请使用console.log()

这篇关于"ajax:成功";被叫两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆