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

时间:2021-08-13 13:00:28

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

这个请求。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"},
]};

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

在上面的方法中,我们用国家代码和图像的名字来描述我们的自我。

I'm looking for a method to use Geonames via http://api.geonames.org/export/geonamesData.js?username=orakzai to retrieve Countrycode and CountryFlags via http://www.geonames.org/flags/x/xx.gif where xx is 2 letter ISO country code

我正在寻找通过http://api.geonames.org/export/geonamesData.js?用户名=orakzai通过http://www.geonames.org/flags/x/xx.gif检索Countrycode和CountryFlags,其中xx是ISO国家代码。

2 个解决方案

#1


24  

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,或者您可以在创建HTML页面时对服务器上的URL进行处理并插入它。

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你可以用id='myImage'修改已经加载的图像标签的URL

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

#2


6  

Similar service, like geonames.org:

类似的服务,比如geonames.org:

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

#1


24  

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,或者您可以在创建HTML页面时对服务器上的URL进行处理并插入它。

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你可以用id='myImage'修改已经加载的图像标签的URL

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

#2


6  

Similar service, like geonames.org:

类似的服务,比如geonames.org:

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