简体   繁体   English

在ajax请求中使用jquery获取JavaScript响应UJS

[英]Using jquery in ajax request to get javascript response UJS

In Rails, you can respond to different MIME types, including application/javascript . 在Rails中,您可以响应不同的MIME类型,包括application/javascript You often use this MIME type for ajax requests to send javascript back to the browser to be evaluated. 您通常将此MIME类型用于ajax请求,以将javascript发送回浏览器进行评估。 The respond_to symbol is :js . respond_to符号是: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. rails.js文件查找远程链接,表单和输入,并覆盖其单击事件以通过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. 在我的情况下,我需要在jQuery中进行请求,因此无法使用remote:true功能。

<%= 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. 我的问题是,当我在jquery ajax方法中将contentType指定为'application/javascript'时,没有表单数据发送到服务器。 I even inspected it in Network tab on chrome. 我什至在chrome的“网络”标签中对其进行了检查。 When I remove the contentType property, the form data is submitted to server but the the content type is not ' application/javascript '. 当我删除contentType属性时,表单数据将提交到服务器,但内容类型不是' application/javascript '。 It is ' application/x-www-form-urlencoded; charset=UTF-8 它是' application/x-www-form-urlencoded; charset=UTF-8 application/x-www-form-urlencoded; charset=UTF-8 '. application/x-www-form-urlencoded; charset=UTF-8 '。

My jquery looks like this: 我的jQuery看起来像这样:

$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'? 如何将内容类型为“应用程序/ javascript”的表单数据发送到服务器?

The contentType option is used to specify the format of the parameters being sent to the controller. contentType选项用于指定要发送到控制器的参数的格式。 To specify the type of data expected in response, use the dataType option; 要指定期望的响应数据类型,请使用dataType选项; 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. 指定script意味着您需要Java script ,它将发送Accept: application/javascript标头,告诉控制器应该使用哪个respond_to块。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM