读取json文件的两种方式:js的XMLHttpRequest 与 jQuery的$.getJSON
作者:互联网
1. jquery方式
$.getJSON(`/map/city/${cName}.json`, data => { that.$echarts.registerMap(param, data); //data是提取成功后返回的数据 alert('县'); initEcharts(param); });
2.js方式
var xmlhttp = new XMLHttpRequest();xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == XMLHttpRequest.DONE) { if (xmlhttp.status == 200) { var resData = xmlhttp.responseText; // xmlhttp.responseText 同上面的 data that.$echarts.registerMap(param, resData); alert('县'); initEcharts(param); } else if (xmlhttp.status == 400) { alert('There was an error 400'); } else { alert('something else other than 200 was returned'); } } }; xmlhttp.open('GET', `/map/city/${cName}.json`, true); xmlhttp.send();
标签:jQuery,xmlhttp,getJSON,param,alert,json,XMLHttpRequest,data 来源: https://www.cnblogs.com/ifeelthecall/p/16550825.html