jQuery 中的 $.getJSON(在 Rails 应用程序中)没有从跨域 Node.js/Express 应用程序/jsonp 读取 JSON?
从谷歌搜索/论坛我认为可能有两个问题,我都不知道如何解决:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉 RoR,但是当您请求
http://nodeapp.com/users?screen_name=s&callback=foo
时,响应应该如下所示::
而不是 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:and not:
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.