rack-jsonp 不适用于简单请求,但适用于 Rails 2.3 中的 .js 调用
我正在使用 Rails 2.3,我决定提供对 JSONP 的支持。 创建了一个全新的应用程序。 然后运行 script/generatescaffold 用户名:string
这是我的整个环境.rb
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
require File.join(File.dirname(__FILE__), 'boot')
require 'rack/contrib'
Rails::Initializer.run do |config|
config.middleware.use 'Rack::JSONP'
end
当我访问 localhost:3000/users 时,我得到的只是一个哈希值。 当我访问 localhost:3000/users.js?callback=show 时,我得到了很好的结果。
让我们看一下 jsonp 代码。 我不明白为什么响应被包装在数组中。
我创建了另一个 Rack 中间件,
[status, headers, [response]]
用此语句替换了此语句
[status, headers, response]
,现在一切正常。
我拒绝相信这是rack-contrib 中的一个错误。
有人可以告诉我为什么响应被包装在数组中以及如何在我的应用程序中使用rack-contrib。
我的应用程序的完整源代码在这里。 只需克隆它并在 localhost:3000 上运行即可。
I am using Rails 2.3 and I decided to provide support for JSONP. Created a brand new application. Then ran script/generate scaffold User name:string
This is my entire environment.rb
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
require File.join(File.dirname(__FILE__), 'boot')
require 'rack/contrib'
Rails::Initializer.run do |config|
config.middleware.use 'Rack::JSONP'
end
When I visit localhost:3000/users all I get is a hash. When I visit localhost:3000/users.js?callback=show then I get good result.
Let's look at the jsonp code . I do not understand why response is being wrapped in an array.
I created another Rack middleware where I replaced this statement
[status, headers, [response]]
with this statement
[status, headers, response]
And now everything is working fine.
I refuse to believe that this is a bug in rack-contrib.
Can someone enlighten me why response is being wrapped in an array and how could I use rack-contrib in my application.
The full source code of my application is here. Just clone it and run on localhost:3000 .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码是错误的。 它应该是这样的:
在参数不包含回调的情况下,它错误地将响应包装在数组中。 在 params 确实包含回调的情况下,它需要将响应包装在数组中的原因是因为 Rack 响应必须响应 .each()。
That code is wrong. Here's what it should be:
It was incorrectly wrapping the response in an array in the case where the params didn't include a callback. The reason it needs to wrap the response in an array in the case where params does include a callback is because Rack responses must respond to .each().