[英]rails 5.2 how to get form-data value in controller
I have a simple HTML form and I'm sending some data to my server by using Fetch API and FormData. 我有一个简单的HTML表单,我通过使用Fetch API和FormData将一些数据发送到我的服务器。
I'm sending role
and user_id
with their values to the server. 我正在将
role
和user_id
及其值发送到服务器。 In controller when I print params
I get: 在我打印
params
控制器中,我得到:
{"-----------------------------1190833731009709688837505639\\r\\nContent-Disposition: form-data; name"=>"\\"role\\"\\r\\n\\r\\nadmin\\r\\n-----------------------------1190833731009709688837505639\\r\\nContent-Disposition: form-data; name=\\"user_id\\"\\r\\n\\r\\n1\\r\\n-----------------------------1190833731009709688837505639--\\r\\n", "controller"=>"users", "action"=>"updaterole", "id"=>"1"}
{“----------------------------- 1190833731009709688837505639 \\ r \\ nConContent-Disposition:form-data; name”=>“\\”角色\\“\\ r \\ n \\ r \\ nadmin \\ r \\ n ----------------------------- 1190833731009709688837505639 \\ r \\ nContent-处理:form-data; name = \\“user_id \\”\\ r \\ n \\ r \\ n1 \\ r \\ n ------------------------- ---- 1190833731009709688837505639 - \\ r \\ n“,”controller“=>”users“,”action“=>”updaterole“,”id“=>”1“}
How can I access and get role
and user_id
value from this? 如何从中访问并获取
role
和user_id
值?
This is my script on client side: 这是我在客户端的脚本:
var form = document.querySelector("#roleForm");
var formdata = new FormData(form);
fetch(url, {
method: "PATCH",
headers: { 'Content-Type':'multipart/form-data' },
body: formdata,
}).then(
response => response.text() // .json(), etc.
// same as function(response) {return response.text();}
).then(
html => {
console.log(html)
}
);
in my case, that it is alredy mentioned in comments, we have incorrect body. 在我的情况下,它在评论中提到,我们有不正确的身体。 i solved it by removing
headers: { 'Content-Type':'multipart/form-data' },
form fetch request.. looks like we dont need add Content-Type to header. 我通过删除
headers: { 'Content-Type':'multipart/form-data' },
来解决它headers: { 'Content-Type':'multipart/form-data' },
表单获取请求..看起来我们不需要将Content-Type添加到标题中。 anyway my issue solved by this . 无论如何我的问题由此解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.