Rails 5:表单如何提交自定义HTTP头?

时间:2022-11-24 08:50:25

I cant seem to find any information on how to get a webform in Rails 5 to submit with custom headers. I would like the URL to which I am sending a PUT request to also receive some custom headers. I am surprised that there is no argument for form_for for this.

我似乎找不到任何关于如何在Rails 5中使用自定义标题提交webform的信息。我希望我发送一个PUT请求的URL也可以接收一些自定义标题。我感到惊讶的是,这里没有form_for参数。

I could accomplish this by submitting the form to an action where I modify the headers there, e.g., request.headers['my-header'] = 'xyz'. I would then have to make the PUT request from within this "middle" controller action, and I feel this additional step is clunky and unconventional.

我可以通过将表单提交给一个操作来实现这一点,在这个操作中,我可以修改那里的header,例如request。头['我打入']=“xyz”。然后,我将不得不在这个“中间”控制器操作中发出PUT请求,我觉得这个额外的步骤是笨拙的和非常规的。

I could also use jQuery to bind to the submit click, and submit the form data after adding the headers via JavaScript. Id rather not involve another layer (i.e., JS) in this process.

我还可以使用jQuery绑定到提交单击,并在通过JavaScript添加头后提交表单数据。我宁愿不涉及另一个层(例如。在这个过程中。

I would rather not do either. Is there a way I can just use the Rails form helpers (or some controller helper) to add some custom headers to the request made by the form submission?

我宁愿不做这两件事。是否有一种方法可以使用Rails表单助手(或控制器助手)向表单提交发出的请求添加一些自定义头?

3 个解决方案

#1


6  

Rails does not have any tags that allows us to do that and therefore cannot add custom headers to your request.

Rails没有任何允许我们这样做的标记,因此不能向您的请求添加自定义头。

In fact, you cannot set custom headers in html forms without xhr plugins, You have to use it with ajax. Something like this:-

事实上,在没有xhr插件的情况下,您不能在html表单中设置自定义标题,您必须在ajax中使用它。这样的:-

<%= form_tag("/your_url", method: :post, :remote => true, :html => { id: "form-id" }) do |f| %>
    ...your form here...
<% end %>

and then you ajax code:-

然后是ajax代码:-。

$('#form-id').submit(function() {
    var valuesToSubmit = $(this).serialize();
    $.ajax({
        type: "POST",
        url: $(this).attr('action'),
        data: valuesToSubmit,
        headers: { 'Xmlrpc-Token': 'value' , 'Token': 'another_value'}
    }).success(function(response){
        //success code
    });
    return false;
});

Using only remote: true in rails will make ajax call, but you want to be able to customize it using the code above.

仅使用remote: true在rails中会进行ajax调用,但您希望能够使用上面的代码对其进行定制。

#2


2  

Browser will send only standard headers like cookies, contenttype, etc. You can not send Authorization header (or other custom) using HTML form submit. You should use AJAX to do that.

浏览器将只发送标准的标头,如cookies、contenttype等。您不能使用HTML表单提交发送授权标头(或其他自定义标头)。您应该使用AJAX实现这一点。

#3


0  

$("#idForm").submit(function(e) {

    var url = "path/to/your/script.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#idForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    e.preventDefault(); // avoid to execute the actual submit of the form.
});
<%= form_tag("/your_url", method: :post, :remote => true, :html => { id: "form-id" }) do |f| %>
    ...your form here...
<% end %>

$("#idForm").submit(function(e) {

    var url = "path/to/your/script.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#idForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    e.preventDefault(); // avoid to execute the actual submit of the form.
});

#1


6  

Rails does not have any tags that allows us to do that and therefore cannot add custom headers to your request.

Rails没有任何允许我们这样做的标记,因此不能向您的请求添加自定义头。

In fact, you cannot set custom headers in html forms without xhr plugins, You have to use it with ajax. Something like this:-

事实上,在没有xhr插件的情况下,您不能在html表单中设置自定义标题,您必须在ajax中使用它。这样的:-

<%= form_tag("/your_url", method: :post, :remote => true, :html => { id: "form-id" }) do |f| %>
    ...your form here...
<% end %>

and then you ajax code:-

然后是ajax代码:-。

$('#form-id').submit(function() {
    var valuesToSubmit = $(this).serialize();
    $.ajax({
        type: "POST",
        url: $(this).attr('action'),
        data: valuesToSubmit,
        headers: { 'Xmlrpc-Token': 'value' , 'Token': 'another_value'}
    }).success(function(response){
        //success code
    });
    return false;
});

Using only remote: true in rails will make ajax call, but you want to be able to customize it using the code above.

仅使用remote: true在rails中会进行ajax调用,但您希望能够使用上面的代码对其进行定制。

#2


2  

Browser will send only standard headers like cookies, contenttype, etc. You can not send Authorization header (or other custom) using HTML form submit. You should use AJAX to do that.

浏览器将只发送标准的标头,如cookies、contenttype等。您不能使用HTML表单提交发送授权标头(或其他自定义标头)。您应该使用AJAX实现这一点。

#3


0  

$("#idForm").submit(function(e) {

    var url = "path/to/your/script.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#idForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    e.preventDefault(); // avoid to execute the actual submit of the form.
});
<%= form_tag("/your_url", method: :post, :remote => true, :html => { id: "form-id" }) do |f| %>
    ...your form here...
<% end %>

$("#idForm").submit(function(e) {

    var url = "path/to/your/script.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#idForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    e.preventDefault(); // avoid to execute the actual submit of the form.
});