如何从外部JSON Feed创建JSONP? [英] How do I create a JSONP from an external JSON feed?

查看:145
本文介绍了如何从外部JSON Feed创建JSONP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个域:www.domain1.com和www.domain2.com

I have two domains: www.domain1.com and www.domain2.com

我在domain1上有一个纯JSON数据源。

I have a plain JSON feed on domain1.

我想从domain1中提取JSON feed,并将其放在domain2上的一个模块上。

I want to pull the JSON feed from domain1 and put it on a module on domain2.

去使用JSONP,但你怎么去这样做呢?有没有办法只用JQuery / javascript?或者我将不得不使用服务器端代码(我使用Coldfusion)。也可以使用.getJSON而不是.ajax(我是初学者,所以我从来没有使用过.ajax)

From what I've read, the way to go about it is by using JSONP but how do you go about doing that? Is there a way to do it with just JQuery/javascript? Or would I have to use server-side code (I'm using Coldfusion). Also could I just use .getJSON and not .ajax (I'm a beginner so I've never used .ajax yet)

EDIT
好​​吧,我还是很困惑。只是在URL的末尾添加回调打破了它。我怎么能这样,我不是一个远程路径的饲料我正在提取一个绝对路径,其中此代码在www.domain2.com,但饲料在www.domain1.com?

EDIT Okay I'm still getting confused. Just adding callback at the end of the url broke it. How could I make it so that instead of a remote path for a feed I am pulling an absolute path where this code is on www.domain2.com but the feed is on www.domain1.com?

var feed ="/event/json.tag/tag/sports/";

$.getJSON(feed,function(data) {

   $.each(data.items, function(i,obj) {

       do something here...
   }        

}


推荐答案

JSONP只是一个环绕JSON对象的回调函数
一般约定是有一个返回JSON的端点,除非在请求上定义了回调参数,并返回JSONP

JSONP is just a callback function wrapped around a JSON object. General convention is to have an endpoint that returns JSON, unless a callback parameter is defined on the request, and returns JSONP in that case.

http:// www。 domain1.com/api/getStuff 可能会返回:

{'foo': 'bar', 'fizz': 'buzz'}

然后 http://www.domain1.com/api/getStuff?cb=cb123 应返回:

cb123({'foo': 'bar', 'fizz': 'buzz'});

我不知道ColdFusion,但我认为这个例子是好的: http://www.coldfusionjedi.com/index.cfm/2009/3/11/Writing-a-JSONP-service-in- ColdFusion

I don't know ColdFusion, but I assume this example is good: http://www.coldfusionjedi.com/index.cfm/2009/3/11/Writing-a-JSONP-service-in-ColdFusion

没有客户端解决方案,除非其他人已经在你正在使用的服务器上构建JSONP支持...

There's no client-only solution unless somebody else already built JSONP support into the server you're working with...

这篇关于如何从外部JSON Feed创建JSONP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆