我正在尝试使用 jquery 解析 json 文件,但我没有得到任何数据,
我的jquery代码:
$(document).ready(function () {
$.ajax({
type: 'GET',
url: 'http://anteatercycles.co.uk/xml/sensa.json',
success: function (json) {
var data1 = json.data[0];
var result1 = '<h3>' + data1.merchant_category + '</h3>' + '<p>' + data1.description + '</p>' ;
$('#description1').append(result1);
}
});
});
<div id="description1"></div>
提前致谢。
编辑到下面仍然没有检索到任何数据?
$(document).ready(function () {
$.ajax({ 类型:'获取', 数据类型:“json”, 网址:'http://anteatercycles.co.uk/xml/sensa.json ', 成功:函数(json) { json=JSON.parse(json);//解析json数据 var data1 = json.data[0]; var result1 = '
' + data1.merchant_category + '
' + '' + data1.description + '
' ; $('#description1').append(result1); } }); });</script>
<div id="description1"></div>
最佳答案
添加 dataType: 'json'
作为 AJAX 调用的选项应该可行。如果你告诉 jQuery 它是 JSON 数据,它应该为你将其解析为一个对象。服务器很可能没有返回具有正确 MIME 类型的 JSON 文件,因此 jQuery 只是假设它是纯文本。
关于javascript - 解析我的 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25664294/