使用jsonp方式的好处是javascript可以跨域访问jsonp服务,只要客户端能连接外网就能访问到,不需要服务器端处理,没有服务器压力。
使用方式,直接访问地址,将得到js,将下面的地址引入到script标签
//指定天气信息给变量方式
http://9snow.org/weather/api?city=北京&var=test1
//指定callback函数方式
http://9snow.org/weather/api?city=北京&callback=test2
只带city参数直接返回城市天气的json数据
展示如下:
指定变量方式:
var test=天气数据
指定callback方式:
test2(天气数据)
数据格式如下:
{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2010年10月30日","date":"庚寅年九月廿三","week":"星期六","fchh":"08","cityid":"101010100","temp1":"18℃~5℃","temp2":"19℃~6℃","temp3":"14℃~3℃","temp4":"14℃~3℃","temp5":"15℃~5℃","temp6":"15℃~5℃","tempF1":"64.4℉~41℉","tempF2":"66.2℉~42.8℉","tempF3":"57.2℉~37.4℉","tempF4":"57.2℉~37.4℉","tempF5":"59℉~41℉","tempF6":"59℉~41℉","weather1":"晴","weather2":"晴转多云","weather3":"多云转晴","weather4":"晴转多云","weather5":"多云转晴","weather6":"晴","img1":"0","img2":"99","img3":"0","img4":"1","img5":"1","img6":"0","img7":"0","img8":"1","img9":"1","img10":"0","img11":"0","img12":"99","img_single":"0","img_title1":"晴","img_title2":"晴","img_title3":"晴","img_title4":"多云","img_title5":"多云","img_title6":"晴","img_title7":"晴","img_title8":"多云","img_title9":"多云","img_title10":"晴","img_title11":"晴","img_title12":"晴","img_title_single":"晴","wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"温凉","index_d":"较凉爽,建议着夹衣加薄羊毛衫等春秋服装。体弱者宜着夹衣加羊毛衫。因昼夜温差较大,注意增减衣服。","index48":"舒适","index48_d":"建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减衣服。","index_uv":"中等","index48_uv":"中等","index_xc":"适宜","index_tr":"很适宜","index_co":"舒适","st1":"19","st2":"7","st3":"18","st4":"7","st5":"13","st6":"5","index_cl":"适宜","index_ls":"适宜"}}
获取到这些数据以后,客户端做处理既可,这种方式可以大大的减少nodejs服务器端的压力,而且可以做成移动的客户端哦,希望对大家有作用,如果有问题,大家可以在下面留言