"ajax:success" being called twice

jquery, ruby-on-rails

Solution

One of a few things are happening.

Either you have multiple elements with .new_item class

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

you are sending the ajax request twice

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

or you are binding the event twice

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

Note: If you are using a modern browser, replace alerts with console.log()

Problem

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. 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!*/ } ``` Anyone have any pointers? Thanks!

Original source

Related problems