Capturing Ajax Success in Rails

After extensive googling, I have been unable to find my mistake. As a beginner with JS, this may be an easy fix for someone with more experience. Working with Rails 4.

I am working on a modal that contains a form and I want to perform certain actions after a successful (or unsuccessful) ajax call, such as closing the modal.

Modal structure (works fine for editing):

.modal.fade{ id: attribute.id, role: 'dialog', tabindex:'-1' }
  .modal-dialog{ role: 'document'}
    .modal-content
      .modal-header
        %button{ type: 'button', class: 'close', 'data-dismiss' => "modal" }
          %span ×
        %h4.modal-title= attribute.name

  = form_for attribute, remote: true, html: { class: 'form-inline', id: 'edit_form' } do |f|
    .modal-body
      .form-group
        = f.label :name, 'Name', class: 'form-label', style: 'width: 150px'
        = f.text_field :name, class: 'form-control', style: 'width:200px;margin-right: 35px'
    .modal-footer
      %button{ type: "button", class: "btn btn-default pull-left", 'data-dismiss' => "modal" }
        Close
      = f.submit 'Create', class: 'btn btn-primary pull-right'

My not-so-impressive JS file:

$(document).ready( function($) {
        console.log('ready'); # visible in console

    $('#edit_form').bind("ajax:success", function() {
      console.log('great success') # *not* visible in console.
    });
});

Controller snippet (interesting fact: Modal triggered in view from different controller than intended one):

respond_to do |format|
  format.js {}
end

I aim to handle the error and success callbacks independently and work with them accordingly (currently investigating this myself). What could be the issue here?

Your assistance is greatly appreciated!

Answer №1

Based on advice from a related thread, it is suggested to attempt the following:

$('#edit_form').on("ajax:success", ...
//             ^-- instead of .bind

If you have an up-to-date jQuery version, try implementing this modification.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble With OnClick and On/Off Class Script Functionality in Chrome and Internet Explorer

I am working on a page with two iframes and a basic navigation bar. The navigation bar links are set up to target one of the iframes and replace its content when clicked. However, I am having trouble highlighting the currently selected link in the iframe - ...

Pause execution of javascript function for a short period

When the button is clicked, my script executes the function manage($n) $onclick = "manage('$n');"; However, I also want to refresh the page immediately after it is clicked. $onclick="window.location.reload(true);manage('$n')"; Altho ...

Continuously incorporating Ajax requests into a queue

I'm currently tackling a new feature at work and find myself at a crucial point. Despite being unfamiliar with Ajax, I am determined to set up a manual queue to handle the requests instead of relying on the browser. After some research and referring t ...

Converting a JavaScript function to work in TypeScript: a step-by-step guide

When writing it like this, using the this keyword is not possible. LoadDrawing(drawing_name) { this.glg.LoadWidgetFromURL(drawing_name, null, this.LoadCB,drawing_name); } LoadCB(drawing, drawing_name) { if (drawing == null) { return; ...

Easiest method for combining elements made in Angular with D3

Exploring various methods to combine D3 and Angular for learning purposes, seeking advice on the approach: The client application is fed a JSON array of nodes and edges from the server. The main component is a graph visualization using D3 force-directed l ...

Synchronous vs Asynchronous: Exploring the Contrasts in Ajax Requests

https://i.sstatic.net/d7yqx.png Furthermore, when there is code following an ajax call, in asynchronous execution it will run ahead of other tasks. Conversely, in synchronous execution, tasks are completed one after the other within the code. ...

What could be causing the side margins on my navbar in mobile view in Bootstrap?

After spending some time working on a simple navbar with CSS styling, I encountered an issue when viewing it in mobile mode. The navbar did not expand to 100% of the screen width and had a margin of about 20px on both sides. CSS .navbar .brand { wi ...

JavaScript can be used to create dynamic frames within a

Can anyone help identify the issue in my code? frame 1 <script type="text/javascript"> var Clicks = 0 ; function AddClick(){ Clicks = Clicks + 1; document.getElementById('CountedClicks').innerHTML = Clicks ; } localStorage.setItem(' ...

Jquery plugin experiencing a malfunction

I am encountering an issue with my custom plugin as I am relatively new to this. My goal is to modify the properties of div elements on a webpage. Here is the JavaScript code I am using: (function($) { $.fn.changeDiv = function( options ) { var sett ...

Highlight dates in Vue.js that are overdue using a date filter

Currently, I have a Vue filter set up to display dates in a visually appealing way. However, I am looking to enhance the filter by adding a feature that would highlight dates in red if they are overdue (meaning the date is earlier than the current date). I ...

Using the data-content attribute with link_to to create a popover in Bootstrap 4

I am looking to include a link in the data-content attribute of my popover. I do not want to use <%= link_to 'Save it to your events', usersavedevents_path, method: :post %> because it is causing overflow on the UI. Here is how it looks in ...

Is there a way to set the content to be hidden by default in Jquery?

Can anyone advise on how to modify the provided code snippet, sourced from (http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide_show), so that the element remains hidden by default? <!DOCTYPE html> <html> <head> <scrip ...

There is no XHR request sent when invoking the http function

I am facing challenges in configuring a service in angular2 to interact with a REST backend. My attempt at setting up a basic example for sending requests to a rest backend and handling the response seems to be on track. The Service is being called correc ...

Is there a way to extract both a and b from the array?

I just started learning programming and I'm currently working on creating an API call to use in another function. However, I've hit a roadblock. I need to extract values for variables a and b separately from the response of this API call: import ...

Adjust the size of h1 headings to be smaller than 768px within the

My goal is to have an h1 header within a row, with font awesome mark quotes displayed on the left and right. I am attempting to resize it once the screen width goes below 768px. Here's what I have so far: @media screen and (max-width: 768px) { h1 ...

What is the method to submit post data using jQuery or JavaScript?

I need help creating a button that can send post data to another location. Is there a way I can achieve this using JavaScript, or Ajax/jQuery? If there are any details missing from my request, please let me know. ...

Change the color of this element and input field background

Having trouble with setting the background color of an input field to red in this code: $(document).ready(function(){ $(".abc").focus(function(){ $(this).attr('background', 'red'); $("label").text('Insert tex ...

The expo-location feature is failing to accurately record and store all of the positions within the array

Incorporating expo-location in my react-native app, I utilize it to track the user's positions and store them in a redux object. While debugging the object reveals that all positions have been successfully inserted, upon retrieving this array, it turn ...

Is there a way to update the background image of a div element through a JavaScript file within a react component?

After spending hours on this issue, I am still stuck and have exhausted all my ideas and research. In my project, I have three buttons that are supposed to change the background image of my site. The background image is linked to the default "App" div elem ...

javascript code for subtracting values in arrays

I am facing a scenario where I have 3 arrays of the same length. My requirement is to automatically subtract the values in the first two arrays without any user input. If the result of subtraction is negative, I need to identify the index of that array num ...