我正在使用 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
因为数组中的这个值为空,Webrick 会抛出以下错误:
Internal Server Error
expected Hash (got Array) for param 0
我真的希望避免循环遍历数据(在实际示例中有多个数组)并更改任何这些空值。有没有更简单的方法来确保数据可以发送到服务器而不会出错?
HTML 错误:
内部服务器错误
参数“0”的预期哈希(得到数组)WEBrick/1.3.1 (Ruby/1.9.3/2012-11-10) 在 localhost: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'