我有一个运行 ruby 1.9.2p320 和 rails 3.0.20 的应用程序(我无法更新这个!)
通过正常的 POST 请求发送常见字符和特殊字符(如 ä ü ö)可以正常工作。通过 AJAX 请求将字符从 A 发送到 Z 也可以正常工作。
但是通过 AJAX 发送特殊字符(如 ä ü ö)会使我的应用程序崩溃。
我的回溯显示问题似乎在于解析 active_support 中的参数。
一个示例请求:
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:60
Content-Type:application/json
X-Requested-With:XMLHttpRequest
Payload: {status_message":{"message":"fghfghfghfhfö"}} # (I found out that it seems to be normal that the encoding messes up here, other rails apps can handle it)
堆栈跟踪是这样说的:
DEBUG rails[14870]: Error occurred while parsing request parameters.
Contents:
FATAL rails[14870]: Encoding::CompatibilityError (incompatible character encodings: UTF-8 and ASCII-8BIT):
activesupport (3.0.20) lib/active_support/json/backends/okjson.rb:330:in `[]='
activesupport (3.0.20) lib/active_support/json/backends/okjson.rb:330:in `unquote'
activesupport (3.0.20) lib/active_support/json/backends/okjson.rb:251:in `strtok'
activesupport (3.0.20) lib/active_support/json/backends/okjson.rb:215:in `tok'
activesupport (3.0.20) lib/active_support/json/backends/okjson.rb:178:in `lex'
activesupport (3.0.20) lib/active_support/json/backends/okjson.rb:46:in `decode'
activesupport (3.0.20) lib/active_support/json/backends/okjson.rb:612:in `decode'
activesupport (3.0.20) lib/active_support/json/decoding.rb:14:in `decode'
actionpack (3.0.20) lib/action_dispatch/middleware/params_parser.rb:47:in `parse_formatted_parameters'
actionpack (3.0.20) lib/action_dispatch/middleware/params_parser.rb:17:in `call'
actionpack (3.0.20) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.20) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.20) lib/action_dispatch/middleware/cookies.rb:302:in `call'
actionpack (3.0.20) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.20) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.20) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.7) lib/rack/sendfile.rb:106:in `call'
actionpack (3.0.20) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.20) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
rack (1.2.7) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.20) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.7) lib/rack/lock.rb:13:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.7) lib/rack/lock.rb:13:in `call'
actionpack (3.0.20) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.20) lib/rails/application.rb:168:in `call'
railties (3.0.20) lib/rails/application.rb:77:in `method_missing'
railties (3.0.20) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.7) lib/rack/content_length.rb:13:in `call'
rack (1.2.7) lib/rack/handler/webrick.rb:52:in `service'
/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
INFO rails[14870]: Rendered /.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.0.20/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
INFO rails[14870]: Rendered /.rvm/gems/ruby-1.9.2-p320/gems/actionpack-3.0.20/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
如何解决这个问题?我不知道如何在 Rails 中更改编码。因为我没有执行此操作的对象。