读取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();

  

posted @ 2022-08-04 15:52  不如饲猪  阅读(477)  评论(0编辑  收藏  举报