是否有一个免费的JSON代理服务器支持CORS或JSONP? [英] Is there a free JSON proxy server which supports CORS or JSONP?

查看:291
本文介绍了是否有一个免费的JSON代理服务器支持CORS或JSONP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个小工具,可以调用只返回 JSON 的在线资源,而不是 JSONP ,但不支持 CORS



因为我的工具将在浏览器中运行,我受到相同的原始策略。 / p>

此外,我没有服务器或域或任何这样的地方设置我自己的代理服务器。 (我没有钱拿到一个。)



我发现至少有两个这样的代理在那里,但似乎要么被打破, t与我尝试接口的资源工作。它称为 AirportCode ,并使用类似如下的网址:


http://airportcode.riobard.com/airport/FRA?fmt=JSON


并返回JSON:

  {code:FRA,name:法兰克福国际机场,location:德国法兰克福}`

有没有人知道这样的代理仍然存在并且与这个AirportCode资源合作?



JSONPwrapper.com 似乎不喜欢此资源, jsonpify.com

解决方案

是的! 雅虎的YQL可用于此!



事实上,YQL不仅支持JSONP - 它甚至支持CORS!



因此,如果你的浏览器也支持CORS,你可以把它想象成一个免费的JSON到JSON代理服务器。如果没有,那么它也是一个免费的JSON到JSONP代理:



这是我使用jQuery管理它:

  $。getJSON(http://query.yahooapis.com/v1/public/yql,
{
q:select * from json where url = \http://airportcode.riobard.com/airport/+ code +?fmt = JSON \,
callback:gotJSON,//甚至不需要这行浏览器支持CORS
格式:json
},
function(data){
if(data.query.results){
/ * do something with
data.query.results.json.code
data.query.results.json.name
data.query.results.json.location
* /
} else {
/ *没有此代码的信息* /
}
}
);

jsfiddle ...


I want to make a little JavaScript tool that can call an online resource that only returns JSON and not JSONP, and does not support CORS.

Because my tool will run in the browser I am subject to the same origin policy.

Also I don't have a server or a domain or any such place to set up a proxy server of my own. (And I don't have any money to get one.)

I have found at least two such proxies out there but both seem either to be broken or just don't work with the resource I'm trying to interface to. It's called AirportCode and takes a URL like this:

http://airportcode.riobard.com/airport/FRA?fmt=JSON

and returns JSON like this:

{"code": "FRA", "name": "Frankfurt International Airport", "location": "Frankfurt, Germany"}`

Does anyone know of such a proxy which still exists and works with this AirportCode resource?

(JSONPwrapper.com doesn't seem to like this resource and jsonpify.com doesn't seem to work at all any more.)

解决方案

Yes! Yahoo's "YQL" can be used for this!

In fact YQL doesn't only support JSONP - it even supports CORS!

So if your browser also supports CORS you can think of it as a free JSON to JSON proxy server. If not, then it is also a free JSON to JSONP proxy:

Here's how I managed it using jQuery:

$.getJSON("http://query.yahooapis.com/v1/public/yql",
  {
    q:      "select * from json where url=\"http://airportcode.riobard.com/airport/" + code + "?fmt=JSON\"",
    callback: gotJSON, // you don't even need this line if your browser supports CORS
    format: "json"
  },
  function(data){
    if (data.query.results) {
      /* do something with
        data.query.results.json.code
        data.query.results.json.name
        data.query.results.json.location
      */
    } else {
      /* no info for this code */
    }
  }
);

And a version on jsfiddle...

这篇关于是否有一个免费的JSON代理服务器支持CORS或JSONP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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