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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14147041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:41:54  来源:igfitidea点击:

Load Country Flag From Geonames API using JSONP technique

jsonweb-servicesapijsonpcountry-codes

提问by Arif

This Request.JSONhttp://mootools.net/demos/?demo=Request.JSONusing JSON data in a way like this,

这个Request.JSONhttp://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"},
]};

In the above method we use Countrycode& imagesby writing name of each image our self.

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

I'm looking for a method to use Geonamesvia http://api.geonames.org/export/geonamesData.js?username=orakzaito retrieve Countrycodeand CountryFlagsvia http://www.geonames.org/flags/x/xx.gifwhere xx is 2 letter ISO country code

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

回答by Lee Meador

The flags are returned as GIF files instead of any sort of JSON. You would just use

这些标志作为 GIF 文件而不是任何类型的 JSON 返回。你会用

<img id='myImage' src="http://www.geonames.org/flags/x/??.gif" />

But fill in the ?? with the country code that geonames uses.

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

You can put the tag in your page somewhere and use some javascript to change the URL to the one you have computed or you can figure the URL on your server and insert it as the HTML page is created.

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

If you want to do it in javascript, for example, in jQuery you would have something like this to change the URL on an already loaded image tag with id='myImage'

例如,如果您想在 javascript 中执行此操作,则在 jQuery 中,您可以使用这样的方法来更改已加载的图像标记上的 URL,其中 id='myImage'

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

回答by Tarampampam

Similar service, like geonames.org:

类似的服务,如 geonames.org:

var country_code = 'uk',
  img_uri = 'https://flagpedia.net/data/flags/normal/' + country_code + '.png';