2013-01-03 188 views
13

Request.JSONhttp://mootools.net/demos/?demo=Request.JSON在这样的方式使用JSON数据,负载国家的国旗国地名API使用JSONP技术

var data = {"previews":[ 
    {"Countrycode":"us", "src":"us.jpg", "description":"desc will be here"}, 
    {"Countrycode":"uk", "src":"uk.jpg", "description":"desc will be here"}, 
]}; 

在上述方法中,我们使用Countrycode & images每个图像我们的自我书写名字。

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

回答

24

的标志返回GIF文件,而不是任何形式的JSON的方法。你只需要使用

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

但是填写??与geonames使用的国家代码。

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

如果你想这样做在JavaScript中,例如,在jQuery的你有这样的事情来改变URL上已加载图像标签使用id =“MYIMAGE”

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

其确定为图像,但如何从http://api.geonames.org/export/geonamesData.js得到'Countrycode' ?username = orakzai – Arif

+0

它的脚本,这里是讲述如何加载脚本的mootools页面:[点击这里](http://mootools.net/docs/more/Utilities/Assets),这里是jQuery [点击这里] {http://api.jquery.com/jQuery.getScript/) –

+1

网址现在采用以下格式:http://geotree.geonames.org/img/flags18/xx.png –

6

类似的服务,像geonames.org:

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

+1

除“uk”不是国家代码。在这种情况下,“gb”会起作用。 –