Javascript is sent every time ajax rails application

I have a rails application that requests cross domain (I use rack-cors to execute this cross domain request without jsonp) through this ajax: it responds with html

  //to ensure cache=true gets passed
  $.ajaxPrefilter('script', function(options) {
  options.cache = true;
  });

   //ajax requests html
   $.ajax({
  beforeSend: function (xhr) {
      xhr.setRequestHeader ('Authorization', api_key);
  },
  dataType: 'html',
  type: 'GET',
  url: url+'/gwsearch/ajax_search?d1='+d1_val+'&d2='+d2_val,
  crossDomain: true,
  success:function(result){

    $("#display").html(result);
  },
  error: function(result) {
    $('#display').html('Unauthorized client.');
  }

It responds with the HTML that is displayed in #display. It has this script tag included in html

  <script src="http://localhost:3000/assets/application.js" type="text/javascript"></script>

I have input buttons in a new displayed rails application, they look like this:

   <%= form_tag plans_collapse_plans_path, :method => 'post', :remote => true do %>
      <%= hidden_field_tag(:plangroup_id, plangroup.id) %>
      <%= image_submit_tag "collapse.png" %>
   <% end %>

Pressing the enter button performs POST. The problem is that the enter button will POST as many times as I asked for the cross-domain application. If I asked the application 5 times, when I press the enter button in the application, it will POST 5 times.

, ajax, application.js; .

EDIT: , , , application.js: cache

collapse_tplans POST application.js, . ajax 6 , 6 POST- collapse_tplan application.js

imagee

, script html- jquery ( jquery application.js), , , , . js , script. , " Chrome", application.js , , ajax.

? POST , . .

+4
3

, application.js . :

///application.html.erb

, application.js

<!--#  <%= javascript_include_tag "application" %> -->

<script src="https://myapp.herokuapp.com/assets/application.js"></script>

, application.js, rails html application.js. , , , !

0

remote: true , , rails.js script. 5 = 5 = 5 ajax.

, :

  • True , , .
  • script.

: ( )

success:function(result){
  $(document).undelegate('submit.rails');
  $("#display").html(result);
},

:

, rails.js , submit.rails. html , rails.js. , , .

+3

@natedavisolds , application.js script 5 , click hanlder button submit form 5 .

, application.js , , .

application.js script:

console.log('application.js executed');

, , .


script, application.js, , , ajax , #display node .

application.js:

  • application.js : , div #display .
  • results.js ( , ).
  • <script src="application.js"> node /gwsearch/ajax_search <script src="results.js"> node
+3

Source: https://habr.com/ru/post/1524424/


All Articles