jQuery 中的 $.getJSON(在 Rails 应用程序中)没有从跨域 Node.js/Express 应用程序/jsonp 读取 JSON?

发布于 2024-09-08 13:53:11 字数 652 浏览 1 评论 0原文

从谷歌搜索/论坛我认为可能有两个问题,我都不知道如何解决:

1)我需要对node.js请求中的jsonp回调做一些事情(它是由jquery b/c自动生成的) callback=? param) - 将其添加到标头(在哪里?以及如何?)或将其添加到 json 响应(同样,在哪里?以及如何?)

2) 保持 Node.js 请求连接可能存在一些问题打开,因此从不发出回调信号?

jquery代码:

url = http://nodeapp.com/users?screen_name=s&callback=?
$.getJSON(this_url, function(data){
     var users = data
     $.each(users, function(i, item){
     $("#users").append(item...);
})

node.js/express.js(咖啡脚本)代码:

get '/user', ->
   users.all... =>
      @header("Content-Type", 'application/javascript')
      @respond 200, JSON.stringify(users)

From googling/forums I think there could be two issues, neither of which I know how to fix:

1) I need to do something with the jsonp callback in the node.js request (which was generated automatically by jquery b/c of the callback=? param) - either add it to the header (where? and how?) or add it to the json response (again, where? and how?)

2) There could be some issue with the node.js request connection staying open, and therefore never signaling the callback?

jquery code:

url = http://nodeapp.com/users?screen_name=s&callback=?
$.getJSON(this_url, function(data){
     var users = data
     $.each(users, function(i, item){
     $("#users").append(item...);
})

node.js/express.js (coffee script) code:

get '/user', ->
   users.all... =>
      @header("Content-Type", 'application/javascript')
      @respond 200, JSON.stringify(users)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

黯然 2024-09-15 13:53:11

我不熟悉 RoR,但是当您请求 http://nodeapp.com/users?screen_name=s&callback=foo 时,响应应该如下所示:

foo({ first_name: 'John', last_name: 'Smith' });

{ first_name: 'John', last_name: 'Smith' }

而不是 code>callback=? 参数不需要修改,jQuery 会传递一个随机值,以便调用匿名回调。

I am not familiar with RoR but when you request http://nodeapp.com/users?screen_name=s&callback=foo, the response should look like this:

foo({ first_name: 'John', last_name: 'Smith' });

and not:

{ first_name: 'John', last_name: 'Smith' }

As far as the callback=? parameter is concerned you don't need to modify it, jQuery will pass a random value so that the anonymous callback could be invoked.

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