How do I append data to a jquery-ujs post request in Rails?

后端 未结 3 1130
难免孤独
难免孤独 2021-02-02 02:37

I have an Ajax form with a data-remote=\"true\" attribute which I\'m submitting to a controller in rails.

What I\'d like to do is to use the jquery-ujs even

相关标签:
3条回答
  • 2021-02-02 03:18

    To change data attribute (at least now, using Jquery 1.7.2) inside the beforeSend method (the Jquery native method), you can just alter the this.data variable.

    0 讨论(0)
  • 2021-02-02 03:23

    I figured this one out myself in the end. It appears that despite what the docs say, the ajax:beforeSend hook actually takes three arguments. According to this helpful blog post they are event, xhr and settingsin that order.

    The form data I was looking for is in the in the data attribute of the settings argument.

    So basically, I can now add data to the request with the function

    $("#my_form").bind('ajax:beforeSend', function(event, xhr, settings){
      settings.data += "&serialized=data";
    });
    

    And the extra paramaters will be available on the server.

    0 讨论(0)
  • 2021-02-02 03:24

    As of the jQuery 1.5, there is a local beforeSend callback that can be bound to your request. Documentation link and another documentation link

    EDIT: The request data is available in the jqXHR object, the first parameter beforeSend callback.

    beforeSend(jqXHR, settings)
    

    Try inspecting the jqXHR object in firebug to see exactly where it stores the POST data and just append your values,

    0 讨论(0)
提交回复
热议问题