如何将json请求发送到rails应用程序

时间:2022-10-24 13:05:15

I wants to send some json parameter from HTML page with text area and I want to send json parameter written in this text area eg format. {"name": "test", "description":"test", "price":100}

我想从带有文本区域的HTML页面发送一些json参数,我想发送在这个文本区域中编写的json参数,例如格式。 {“name”:“test”,“description”:“test”,“price”:100}

But rails consider it as {"{'name' : 'test', 'description' : 'test', 'price' : 100}"=>nil} how do I send json parameter from text area to be readable from rails application.

但rails认为它是{“{'name':'test','description':'test','price':100}”=> nil}如何从文本区域发送json参数以便从rails应用程序中读取。

2 个解决方案

#1


0  

I think you need to parse that string on server side with JSON.parse

我认为您需要使用JSON.parse在服务器端解析该字符串

param = '{"name" : "test", "description" : "test", "price" : 100}'
# assume you got the param on server side
json_param = JSON.parse(param)
puts json_param['name']

#2


0  

As you stated in the comment:

正如你在评论中所说:

You can easily understand from this sample:

您可以从此示例中轻松了解:

<input type='textarea' name='content' value='{"name": "test", "description":"test", "price":100}' id='text_content' />

You just sent a string to Rails in this way.

您刚刚以这种方式向Rails发送了一个字符串。

If you do the submit with a form, you should use different inputs for different params.

如果您使用表单进行提交,则应对不同的参数使用不同的输入。

If Javascript, maybe jQuery, you can do it with function $.post.

如果是Javascript,也许是jQuery,你可以用函数$ .post来做。

Rails would parse the json for you.

Rails会为你解析json。

#1


0  

I think you need to parse that string on server side with JSON.parse

我认为您需要使用JSON.parse在服务器端解析该字符串

param = '{"name" : "test", "description" : "test", "price" : 100}'
# assume you got the param on server side
json_param = JSON.parse(param)
puts json_param['name']

#2


0  

As you stated in the comment:

正如你在评论中所说:

You can easily understand from this sample:

您可以从此示例中轻松了解:

<input type='textarea' name='content' value='{"name": "test", "description":"test", "price":100}' id='text_content' />

You just sent a string to Rails in this way.

您刚刚以这种方式向Rails发送了一个字符串。

If you do the submit with a form, you should use different inputs for different params.

如果您使用表单进行提交,则应对不同的参数使用不同的输入。

If Javascript, maybe jQuery, you can do it with function $.post.

如果是Javascript,也许是jQuery,你可以用函数$ .post来做。

Rails would parse the json for you.

Rails会为你解析json。