js获取json数据的方法
1、Ajax获取json数据
$.ajax({type: "get",url: "json/test.json",dataType: "json",async: false,success: function(data) {console.log(data);}});或$.get("json/test.json", function(data) {console.log(data);});
使用 getJSON 方式获取
$.getJSON("json/test.json", function(data) {console.log(data.total);})
以上方式获取到的数据都是 JSON 格式的数据,无需转换,即可使用
例子:
$.getJSON("http://www.域名.com/plus/chengshi.php", function(data) {var str = '';for(var i = 0;i<data.length;i++){var str = '<a href="'+data[i][1]+'">'+data[i][0]+'</a>'+str;}$(".chengshi").append(str);})
推荐文章:
外部函数获取ajax返回的数据
PHP数组与JSON互相转换