使用JSONP技术从Geonames API加载国家/地区标志

Ari*_*rif 15 api json jsonp web-services country-codes

这个Request.JSON http://mootools.net/demos/?demo=Request.JSON以这样的方式使用JSON数据,

var data = {"previews":[
  {"Countrycode":"us", "src":"us.jpg", "description":"desc will be here"},
  {"Countrycode":"uk", "src":"uk.jpg", "description":"desc will be here"},
]};
Run Code Online (Sandbox Code Playgroud)

在上面的方法中,我们使用Countrycode&images通过写每个图像的名称我们自己.

我正在寻找一种方法,Geonames通过http://api.geonames.org/export/geonamesData.js?username=orakzai来检索CountrycodeCountryFlags通过http://www.geonames.org/flags/x/xx.gif其中xx是2个字母的ISO国家代码

Lee*_*dor 24

标志作为GIF文件而不是任何种类的JSON返回.你只需要使用

<img id='myImage' src="http://www.geonames.org/flags/x/??.gif" />
Run Code Online (Sandbox Code Playgroud)

但填写?? ?? 使用geonames使用的国家/地区代码.

您可以将标记放在页面的某个位置,并使用一些javascript将URL更改为您计算的URL,或者您可以在服务器上计算URL并在创建HTML页面时插入它.

如果你想在javascript中这样做,例如,在jQuery中你会有这样的东西来改变已加载的图像标签上的URL,id ='myImage'

 $("#myImage").attr('src', "http://www.geonames.org/flags/x/" + countryCode + ".gif")
Run Code Online (Sandbox Code Playgroud)


Par*_*tаm 7

类似的服务,如geonames.org:

var country_code = 'uk',
  img_uri = 'https://flagpedia.net/data/flags/normal/' + country_code + '.png';
Run Code Online (Sandbox Code Playgroud)

  • 但“uk”不是国家/地区代码。“gb”在这种情况下可以工作。 (2认同)