读取json文件只适合同源方式,即ajax方式。

读取json数据方式有多种,请求url必须是jsp,php等能够获取请求参数,返回响应的文件。

 

原生js实现

 

function ajaxRequest(type, url, callback) {

var
type = type,
url = url,
callback = callback;

var xmlhttp = new XMLHttpRequest();
xmlhttp.open(type,url, true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
callback(json_encode(xmlhttp.responseText))
}
}
};
}

function json_encode(str) {
json = JSON.parse(str);
return json;
}


ajaxRequest('get','json/data.json',function(data){
console.log(data);
});

 

jquery实现

 

 $.getJSON('http://localhost:8080/json/data.json',function(data){
       console.log(data);
    });
 $.ajax({
        url: 'json/data.json',
        success: function (result) {
            console.log(result);
        }
    });

 

 

Extjs实现

 

Ext.Ajax.request({
        url: 'json/data.json',
        method: 'GET',
        success: function (result) {
           console.log(result.responseText);
        },
        failure: function (response) {
            console.log(response.status);
        }
    });

 

 

 

 


 
                    
            
                

相关文章:

  • 2022-12-23
  • 2021-08-22
  • 2021-12-09
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-25
  • 2021-12-01
  • 2021-08-01
  • 2022-12-23
相关资源
相似解决方案