I'm sending an AJAX request and posting some json data to the server. How do I access this data in my controller (I'm using Ruby on Rails).
我将发送一个AJAX请求并向服务器发布一些json数据。如何在我的控制器中访问这些数据(我正在使用Ruby on Rails)。
3 个解决方案
#1
3
It should be trivial:
应该是微不足道的:
data = ActiveSupport::JSON::decode(params[:param_with_json_string])
#2
0
If you set the Content-Type of the post request to application/json
then Rails will handle things for you and make the data available in the params hash like normal.
如果您将post请求的内容类型设置为application/json,那么Rails将为您处理事务,并像往常一样在params散列中提供数据。
Example using jQuery:
使用jQuery示例:
$.ajax({
url:'/some_url',
data: {user: {name:"Bob", email: "bob@example.com"}},
dataType: 'json',
type: 'POST'
});
#3
0
Access the parameter through the params hash. There should be no need to decode JSON. Rails should convert it into a hash for you.
通过params散列访问参数。不需要解码JSON。Rails应该将它转换为散列。
#1
3
It should be trivial:
应该是微不足道的:
data = ActiveSupport::JSON::decode(params[:param_with_json_string])
#2
0
If you set the Content-Type of the post request to application/json
then Rails will handle things for you and make the data available in the params hash like normal.
如果您将post请求的内容类型设置为application/json,那么Rails将为您处理事务,并像往常一样在params散列中提供数据。
Example using jQuery:
使用jQuery示例:
$.ajax({
url:'/some_url',
data: {user: {name:"Bob", email: "bob@example.com"}},
dataType: 'json',
type: 'POST'
});
#3
0
Access the parameter through the params hash. There should be no need to decode JSON. Rails should convert it into a hash for you.
通过params散列访问参数。不需要解码JSON。Rails应该将它转换为散列。