【问题标题】:Can't send complex json data to server using prototype and rails无法使用原型和 Rails 将复杂的 json 数据发送到服务器
【发布时间】:2009-07-05 13:00:27
【问题描述】:

我正在尝试将以下数据发送到服务器,但在服务器端我没有看到我正在发送的 json 参数。谁能告诉我我做错了什么?

您可以在下面看到代码。

原型:

send_data_url = "change"  

hash = $H({"blocks": [{"h": "2", "area": [{"width": "96%", "els": [{"title": "first", "mand": true}, {"title": "second", "mand": false}]}]}]});  

var request_array_json = hash.toJSON();  

new Ajax.Request(send_data_url, {  
  method: 'post',  
  parameters: request_array_json,  
  contentType: 'application/json;'  
});  

导轨:

def change()  
   debugger  
   my_json = ActiveSupport::JSON.decode(params["_json"])  
end  

在控制器中,我看到 params 对象没有 json 参数。
它只显示动作和控制器:

{"action"=>"change", "id"=>nil, "controller"=>"default/test"}  

注意:我使用的是原型 1.6 qnd rails 2.3.2。

【问题讨论】:

    标签: ruby-on-rails json prototypejs


    【解决方案1】:

    我找到了一个使用名为 json_request 的插件的解决方案。

    为了让它正常工作,我还用一个可以发送复杂 JSON 对象的新类包装了 Ajax.Request。示例代码:

    Ajax.JSON = Class.create(Ajax.Request, {
      initialize: function($super, url, options) {
        options = options || {};
        options.contentType = ‘application/x-www-form-urlencoded’;
        options.postBody = Object.toJSON(options.object);
        $super(url, options);
      }
    });
    
    new Ajax.JSON(url, { object: myObject });
    

    阅读更多:http://labnotes.org/2007/12/11/json_request-handling-json-request-in-rails-20/comment-page-1/#comment-143190#ixzz0n5iKnO60

    【讨论】:

      猜你喜欢
      • 2015-01-06
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      相关资源
      最近更新 更多