Using jquery in ajax request to get javascript response UJS

Donato

In Rails, you can respond to different MIME types, including application/javascript. You often use this MIME type for ajax requests to send javascript back to the browser to be evaluated. The respond_to symbol is :js.

I see examples like this:

<%= form_tag('/articles', remote: true) do %>
  ...
<% end %>

<%= link_to "Delete article", @article, remote: true, method: :delete %>

The rails.js file finds the remote links, forms, and inputs, and overrides their click events to submit to the server via AJAX.

Server has something like this:

  respond_to do |format|
    format.js 
  end

In my situation, I need to do the request in jQuery, so I cannot use the remote: true feature.

<%= form_for @tag do |f| %>
  <%= f.fields_for :taggings, f.object.taggings.build do |taggings_builder| %>
    <%= taggings_builder.hidden_field :tagging_flag, value: @contact.flag %>
  <% end %>
  <%= f.text_field :name, class: 'form-control', placeholder: 'Add a tag and press enter' %>
<% end %>

My problem is when I specify contentType as 'application/javascript' in jquery ajax method, no form data is sent to server. I even inspected it in Network tab on chrome. When I remove the contentType property, the form data is submitted to server but the the content type is not 'application/javascript'. It is 'application/x-www-form-urlencoded; charset=UTF-8'.

My jquery looks like this:

$view.on('keypress','form#add-tag input#tag_name', function(e){
   var $form = $(this).closest('form');
   var key = e.which;

   if(key == 13){

      $.ajax({
         type: "POST",
         data: $form.serialize(),
         contentType: 'application/javascript',
         url: $form.attr('action'),
         beforeSend: function() {
           $('.loading').show();
         },
         complete: function(){
           $('.loading').hide();
         },
         success: function(resp){
         }
      });


     return false;
   }
})

How can I send the form data to server with the content type of 'application/javascript'?

Barmar

The contentType option is used to specify the format of the parameters being sent to the controller. To specify the type of data expected in response, use the dataType option; specifying script means you expect Javascript, and this will send the Accept: application/javascript header that tells the controller which respond_to block should be used.

  $.ajax({
     type: "POST",
     data: $form.serialize(),
     dataType: 'script',
     url: $form.attr('action'),
     beforeSend: function() {
       $('.loading').show();
     },
     complete: function(){
       $('.loading').hide();
     },
     success: function(resp){
     }
  });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the response text from an AJAX request using jQuery

From Dev

Ajax callback not happening while using jquery-ujs in Rails

From Dev

how do i get a response from a jquery ajax request

From Dev

How to call ajax request with json response using jquery?

From Dev

JQuery Ajax: assign response to two variables using a single request

From Dev

How to show json response data in datatable using jQuery ajax request?

From Dev

jQuery and Rails ajax request and response

From Dev

AJAX JQuery request with XML response

From Dev

jQuery AJAX request not receiving response

From Dev

jQuery and Rails ajax request and response

From Dev

get response after ajax request

From Dev

Javascript to jquery Ajax request

From Dev

Can't get response after click on a link using jquery/ajax

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

Submit Form using JQuery Ajax and get xml response

From Dev

How to Make an AJAX HTTPS GET Request Using jQuery

From Dev

jQuery AJAX request getting response but no callbacks are fired

From Dev

jQuery AJAX request getting response but no callbacks are fired

From Dev

How to add custom header in ajax request using plain javascript (NOT JQUERY)?

From Dev

send ajax get request to node, response is undefined

From Dev

How to get the data response from ajax request?

From Java

Unable to get JSON response form Ajax request

From Dev

How to get the data response from ajax request?

From Dev

jquery: ajax get request not working

From Dev

Jquery .ajax get request with Odata

From Dev

Why does my jQuery Ajax GET-request not receive a response from the server?

From Dev

No response from API using AJAX request

From Dev

Using $.ajax GET not submitting request

Related Related

  1. 1

    How to get the response text from an AJAX request using jQuery

  2. 2

    Ajax callback not happening while using jquery-ujs in Rails

  3. 3

    how do i get a response from a jquery ajax request

  4. 4

    How to call ajax request with json response using jquery?

  5. 5

    JQuery Ajax: assign response to two variables using a single request

  6. 6

    How to show json response data in datatable using jQuery ajax request?

  7. 7

    jQuery and Rails ajax request and response

  8. 8

    AJAX JQuery request with XML response

  9. 9

    jQuery AJAX request not receiving response

  10. 10

    jQuery and Rails ajax request and response

  11. 11

    get response after ajax request

  12. 12

    Javascript to jquery Ajax request

  13. 13

    Can't get response after click on a link using jquery/ajax

  14. 14

    How to get the ajax response from success and assign it in a variable using jQuery?

  15. 15

    How to get the ajax response from success and assign it in a variable using jQuery?

  16. 16

    Submit Form using JQuery Ajax and get xml response

  17. 17

    How to Make an AJAX HTTPS GET Request Using jQuery

  18. 18

    jQuery AJAX request getting response but no callbacks are fired

  19. 19

    jQuery AJAX request getting response but no callbacks are fired

  20. 20

    How to add custom header in ajax request using plain javascript (NOT JQUERY)?

  21. 21

    send ajax get request to node, response is undefined

  22. 22

    How to get the data response from ajax request?

  23. 23

    Unable to get JSON response form Ajax request

  24. 24

    How to get the data response from ajax request?

  25. 25

    jquery: ajax get request not working

  26. 26

    Jquery .ajax get request with Odata

  27. 27

    Why does my jQuery Ajax GET-request not receive a response from the server?

  28. 28

    No response from API using AJAX request

  29. 29

    Using $.ajax GET not submitting request

HotTag

Archive