Webrick用jquery ajax期待Hash(得到数组)

时间:2013-01-23 06:51:58

标签: javascript jquery ruby-on-rails

我正在使用Handsontable jquery插件来更改数据网格中的单元格。 Handsontable将数据发送到数组中:

var change = [['Z', null, 'X']]

然后我使用ajax将此数据传递给服务器:

$.ajax({
  url: "/incomes",
  dataType: "text",
  type: "POST",
  data: { data: change },
});

问题是,如果使用空值,ajax将忽略此值并更改数组,如下所示:

change[0][0]    Z
change[0][1]    
change[0][2]    X

因为数组中的这个值为null,所以Webrick会抛出以下错误:     Internal Server Error     expected Hash (got Array) for param 0

我真的希望避免循环数据(实际例子中有多个数组)并更改任何这些空值。有没有更简单的方法来确保数据可以发送到服务器而不会错误?

HTML错误:

内部服务器错误

参数'0'的预期哈希(得到数组)
WEBrick / 1.3.1(Ruby / 1.9.3 / 2012-11-10)at 本地主机:3000

终端错误:

ERROR TypeError: expected Hash (got Array) for param `0'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/utils.rb:127:in `normalize_params'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/utils.rb:128:in `normalize_params'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/utils.rb:96:in `block in parse_nested_query'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/utils.rb:93:in `each'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/utils.rb:93:in `parse_nested_query'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/request.rb:332:in `parse_query'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/request.rb:209:in `POST'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/methodoverride.rb:26:in `method_override'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/methodoverride.rb:14:in `call'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/runtime.rb:17:in `call'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.9/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/lock.rb:15:in `call'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.9/lib/action_dispatch/middleware/static.rb:62:in `call'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/engine.rb:479:in `call'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/application.rb:223:in `call'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/content_length.rb:14:in `call'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/rack/log_tailer.rb:17:in `call'
    /home/g/.rvm/gems/ruby-1.9.3-p327/gems/rack-1.4.4/lib/rack/handler/webrick.rb:59:in `service'
    /home/g/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
    /home/g/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
    /home/g/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

1 个答案:

答案 0 :(得分:3)

这是一个黑暗中的刺,但您可以尝试使用JSON或其他一些内容类型进行POST:

$.ajax({
  url: "/incomes",
  dataType: "text",
  type: "POST",
  data: JSON.stringify({ data: change }),
  contentType: 'application/json'
});

我建议将此作为一种解决方案,因为JSON格式可以比默认的x-www-form-urlencoded格式更自然地表示空值和嵌套数组(IMO)。

如何运作

JSON.stringify将确保您的数据序列化为JSON。使用这样的contentType设置将导致HTTP请求中的Content-Type标头为“application / json”。 Rails使用它作为线索将传入的POST主体解释为JSON。