Sending JSON string as parameter to action

I have a button in a Ruby on Rails view file.

First, when the button is pressed, an Ajax call is made, which in response gives a JSON string. So far I have completed this task.

This is where I am stuck: the button also redirects me to another action of the same controller. Now I want to send the JSON string received by JavaScript as a parameter to this action.

Is there any way around this?

+3
source share
1 answer

Your ajax call should look something like this:

... doing stuff before ...

$.ajax(
  url:'url-to-script',
  success: function(data) {
    var json = JSON.parse(data);
    $('#hidden-field').val(json); // set a hidden field in your form
    $('#from-id').submit();
  }
);

$('#submit-button').attr('disabled', 'true'); // disable the button
// set some spinny animation to notify the user that you are waiting.
// time out after a while if you don't get a response

... doing stuff after ...

ajax, , . , . - ajax , . , .

+3

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


All Articles