来自 jQuery 的 $.getJSON(在 Rails 应用程序中)没有从跨域 Node.js/Express 应用程序/jsonp 读取 JSON?
Posted
技术标签:
【中文标题】来自 jQuery 的 $.getJSON(在 Rails 应用程序中)没有从跨域 Node.js/Express 应用程序/jsonp 读取 JSON?【英文标题】:$.getJSON from jQuery (in Rails app) not reading JSON from cross-domain Node.js/Express app / jsonp? 【发布时间】:2011-03-14 05:08:13 【问题描述】:从谷歌搜索/论坛我认为可能有两个问题,我都不知道如何解决:
1) 我需要对 node.js 请求中的 jsonp 回调做一些事情(它是由回调的 jquery b/c 自动生成的=?参数) - 或者将它添加到标题中(在哪里?以及如何? ) 或将其添加到 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)
【问题讨论】:
【参考方案1】:我不熟悉 RoR,但是当您请求 http://nodeapp.com/users?screen_name=s&callback=foo
时,响应应该如下所示:
foo( first_name: 'John', last_name: 'Smith' );
而不是:
first_name: 'John', last_name: 'Smith'
callback=?
参数不需要修改,jQuery会传递一个随机值,这样就可以调用匿名回调了。
【讨论】:
以上是关于来自 jQuery 的 $.getJSON(在 Rails 应用程序中)没有从跨域 Node.js/Express 应用程序/jsonp 读取 JSON?的主要内容,如果未能解决你的问题,请参考以下文章
Jquery getJSON 自动按 id 对我的数据进行排序