rack-jsonp 不适用于简单请求,但适用于 Rails 2.3 中的 .js 调用

发布于 2024-07-27 00:24:29 字数 1115 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2024-08-03 00:24:29

该代码是错误的。 它应该是这样的:

def call(env)
  status, headers, response = @app.call(env)
  request = Rack::Request.new(env)
  if request.params.include?('callback')
    response = [pad(request.params.delete('callback'), response)]
    headers['Content-Length'] = response.length.to_s
  end
  [status, headers, response]
end

在参数不包含回调的情况下,它错误地将响应包装在数组中。 在 params 确实包含回调的情况下,它需要将响应包装在数组中的原因是因为 Rack 响应必须响应 .each()。

That code is wrong. Here's what it should be:

def call(env)
  status, headers, response = @app.call(env)
  request = Rack::Request.new(env)
  if request.params.include?('callback')
    response = [pad(request.params.delete('callback'), response)]
    headers['Content-Length'] = response.length.to_s
  end
  [status, headers, response]
end

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文