首页 > json格式天气数据的获取,jsonp方式获取统计图数据

json格式天气数据的获取,jsonp方式获取统计图数据

 获取天气情况(不支持跨域)

/*json原生获取*/
function getJSON() {var XML;var url = "http://wthrcdn.etouch.cn/weather_mini?city=杭州";if (window.XMLHttpRequest) {XML = new XMLHttpRequest();} else if (window.ActiveXObject) {XML = new window.ActiveXObject();} else {alert("请升级至最新版本的浏览器");}if (XML != null) {XML.open("GET", url, true);/*GET可以改成POST,参数在url后面用&连接即可*/XML.send(null);XML.onreadystatechange = function () {if (XML.readyState === 1 || XML.readyState === 2 || XML.readyState === 3) {console.log("发起 " + url + " 数据请求!");}if (XML.readyState === 4 && XML.status === 200) {var result = JSON.parse(XML.responseText).data;alert(result.forecast[0].high);//读取完数据了....}};}
}onload = function () {getJSON();
};

通过jsonp方式获取(支持跨域)

/*jsonp格式数据读取*/
function jsonpCallback(result) {//设置加载动画样式myChart.showLoading('default', {text: '数据统计中…',color: 'blue',textColor: 'blue',maskColor: 'rgba(0, 0, 0, 0.5)',zlevel: 0});//清除原始静态数据option.series[0].data = [];option.xAxis.data = [];myChart.setOption(option);//加载读取的数据到chartfor (var i in result) {var data = result[i];option.series[0].data.push(data.value);option.xAxis.data.push(data.name);}myChart.setOption(option);myChart.hideLoading();
}var callback = 'jsonpCallback';//这个方法名必须要和上面的保持一致
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
var ip = 'http://106.14.xxx.61:8080';
var path = '/bigdata/echart/datapush';
var method = 'originCityDistribution';
var date = new Date()
var yearMonth = '201705';//目前只有2017.05的数据
//var yearMonth = date.getFullYear().toString() + ("0" + (date.getMonth() + 1)).slice(-2);
var time = date.getTime();
script.src = ip + path + '?method=' + method + '&callback=' + callback + '&time=' + yearMonth + '&_=' + time;
head.appendChild(script);

 

更多相关: