首页 新闻 赞助 找找看

json数据显示

0
悬赏园豆:10 [已解决问题] 解决于 2020-03-13 16:25



<button type="button" id="show">省</button>
<div id="apa"></div>
<button type="button" id="">省</button>
<button type="button" id="">省</button>
<script>
$(function () {
$("#show").click(function () {
$.ajax({
dataType: 'json', //服务器返回json格式数据
type: 'get', //HTTP请求类型
timeout: 10000,
//超时时间设置为10秒;
url:'http://levelapi.elizhuo.cn/api/common/getCountryKeyValueList',
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
})
});
我要怎么把获取的数据显示在页面上
我试了for和document都报错了

小白萝卜呀的主页 小白萝卜呀 | 初学一级 | 园豆:4
提问于:2020-03-11 16:47
< >
分享
最佳答案
1

解決了嗎?
還沒的話源碼貼上來看看吧

收获园豆:10
RosonJ | 老鸟四级 |园豆:4910 | 2020-03-13 16:09

已经可以了,我贴一下我的源码吧,方便其他人浏览,麻烦您了

小白萝卜呀 | 园豆:4 (初学一级) | 2020-03-13 16:18
其他回答(3)
1
风行天下12 | 园豆:3867 (老鸟四级) | 2020-03-11 18:29


您好,请问一下这个是本地的文件路径吗,是否可以换成url链接吗

支持(0) 反对(0) 小白萝卜呀 | 园豆:4 (初学一级) | 2020-03-11 18:54

@小白萝卜呀: 换成后台服务端接口地址就行

支持(0) 反对(0) 风行天下12 | 园豆:3867 (老鸟四级) | 2020-03-11 18:57

@小白萝卜呀: 可以换成请求的服务端url

支持(0) 反对(0) 风行天下12 | 园豆:3867 (老鸟四级) | 2020-03-11 18:58
0

可以定义变量,组织好数据格式后,再显示:
$.ajax({
dataType: 'json', //服务器返回json格式数据
type: 'get', //HTTP请求类型
timeout: 10000,
//超时时间设置为10秒;
url:'http://levelapi.elizhuo.cn/api/common/getCountryKeyValueList',
success: function (data) {
console.log(data);
var temp = "";
$.getJson("src",function(){
$.each(result, function(i, field){
temp += field;
});
});
$("spa").html(temp):
},

怪侠Kevin | 园豆:194 (初学一级) | 2020-03-12 10:24
0

这个是解决后的源码
$(function () {
queryCountry();
});
function queryCountry(){
$.ajax({
dataType: 'json', //服务器返回json格式数据
type: 'get', //HTTP请求类型
timeout: 10000,
//超时时间设置为10秒;
url:'http://levelapi.elizhuo.cn/api/common/getCountryKeyValueList',
success: function (data) {
var ahtml = "<ul>";
var jsonObj = eval(data);
for(var p in jsonObj){//遍历json数组时,这么写p为索引,0,1
//console.log(jsonObj[p].key + " " + jsonObj[p].value)
ahtml += "<li><a href='' onclick='selectCity("+jsonObj[p].key+")'>"+jsonObj[p].value+"</a></li>"
}
ahtml += "</ul>"
$("#apa").html(ahtml)
},
error: function (data) {
console.log(data);
}
});

小白萝卜呀 | 园豆:4 (初学一级) | 2020-03-13 16:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册