如何在rails-ujs Rails.ajax POST调用中发送JSON数据(不使用jQuery)?

时间:2021-11-04 01:49:31

I have a React client app that needs to talk to a Rails API. I want to use the rails-ujs method Rails.ajax. For example:

我有一个需要与Rails API通信的React客户端应用程序。我想使用rails-ujs方法Rails.ajax。例如:

Rails.ajax({
  type: "POST", 
  url: "/things",
  data: mydata,
  success: function(repsonse){...},
  error: function(repsonse){...}
})

It looks like I can't set data to a JSON object like this:

看起来我无法像以下那样将数据设置为JSON对象:

mydata = {
 thing: {
  field1: value1,
  field2: value2,
}}

I need to convert it to a application/x-www-form-urlencoded content type manually like this:

我需要手动将它转换为application / x-www-form-urlencoded内容类型,如下所示:

mydata = 'thing[field1]=value1&thing[field2]=value2'

This is ok for flat data but gets complicated quickly for nested data.

这对于平面数据是好的,但对于嵌套数据来说很快就会变得复杂。

jQuery does the conversion automatically before making a request.

jQuery在发出请求之前自动进行转换。

So I'm wondering if Rails UJS has some automatic way of doing it, but I couldn't find anything in the docs or code.

所以我想知道Rails UJS是否有一些自动方式,但我在文档或代码中找不到任何东西。

3 个解决方案

#1


2  

I used ajax call several times and I used to send json data like this.

我多次使用ajax调用,而且我曾经像这样发送json数据。

var fd = new FormData();
fd.append("jsondata", JSON.stringify(mydata));

$.ajax({
  url: ...
  type :"post",
  data: fd
  success:function(){
  }
})

Parsing Json data in ruby controller is easy. you can just use JSON.parse(params["jsondata"])
Hope this works for your case.

在ruby控制器中解析Json数据很容易。你可以使用JSON.parse(params [“jsondata”])希望这适合你的情况。

#2


1  

I reviewed the library code and It doesn't support that because in order to send an object you have to stringify the object, and after doing that I discovered that the library checks if the data is the type of string It changes the content type of the request to application/x-www-form-urlencoded . This link shows that condition in the library. And for that, your option to overcome this issue is to write a method that transforms the object to input form way or use JQuery Ajax or another library.

我查看了库代码并且不支持它,因为为了发送对象,你必须对对象进行字符串化,并且在这之后我发现库检查数据是否是字符串的类型它改变了内容类型对application / x-www-form-urlencoded的请求。此链接显示库中的条件。为此,您选择克服此问题的方法是编写一个方法,将对象转换为输入格式或使用JQuery Ajax或其他库。

#3


0  

Not sure if I'm misunderstanding the issue, but can't you just convert your object using JSON.stringify?

不确定我是否误解了这个问题,但是你不能只使用JSON.stringify转换你的对象吗?

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Certainly I can see in the rails-ujs module it's using JSON.parse to parse the Json response

当然我可以在rails-ujs模块中看到它使用JSON.parse来解析Json响应

#1


2  

I used ajax call several times and I used to send json data like this.

我多次使用ajax调用,而且我曾经像这样发送json数据。

var fd = new FormData();
fd.append("jsondata", JSON.stringify(mydata));

$.ajax({
  url: ...
  type :"post",
  data: fd
  success:function(){
  }
})

Parsing Json data in ruby controller is easy. you can just use JSON.parse(params["jsondata"])
Hope this works for your case.

在ruby控制器中解析Json数据很容易。你可以使用JSON.parse(params [“jsondata”])希望这适合你的情况。

#2


1  

I reviewed the library code and It doesn't support that because in order to send an object you have to stringify the object, and after doing that I discovered that the library checks if the data is the type of string It changes the content type of the request to application/x-www-form-urlencoded . This link shows that condition in the library. And for that, your option to overcome this issue is to write a method that transforms the object to input form way or use JQuery Ajax or another library.

我查看了库代码并且不支持它,因为为了发送对象,你必须对对象进行字符串化,并且在这之后我发现库检查数据是否是字符串的类型它改变了内容类型对application / x-www-form-urlencoded的请求。此链接显示库中的条件。为此,您选择克服此问题的方法是编写一个方法,将对象转换为输入格式或使用JQuery Ajax或其他库。

#3


0  

Not sure if I'm misunderstanding the issue, but can't you just convert your object using JSON.stringify?

不确定我是否误解了这个问题,但是你不能只使用JSON.stringify转换你的对象吗?

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Certainly I can see in the rails-ujs module it's using JSON.parse to parse the Json response

当然我可以在rails-ujs模块中看到它使用JSON.parse来解析Json响应