Javascript 无法使用Ajax和jQuery在div部分显示Json数据
我创建了一个json格式的外部php文件:Javascript 无法使用Ajax和jQuery在div部分显示Json数据,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,我创建了一个json格式的外部php文件: [ { "title": "Welcome!", "description": "The world has changed dramatically..", "image": "img/strawberry-wallpaper.jpg" } ] 我使用此选项显示特定div部分的数据,但没有结果: <script type="text/javascript"> $(document).ready(functio
[
{
"title": "Welcome!",
"description": "The world has changed dramatically..",
"image": "img/strawberry-wallpaper.jpg"
}
]
我使用此选项显示特定div部分的数据,但没有结果:
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: 'json.php',
type: 'post',
data: { get_param: 'value' },
contentType:"application/json; charset=utf-8",
dataType: 'json',
success: function(data){
var obj = jQuery.parseJSON(data);
if(obj.success){
$.each(obj, function (index, item) {
if ('success'!= index){
$('#output').append("<div class='col-md-6' ><img class='img-rounded' src="+item.image+"alt='MyImage' width='550px' height='240px'></div><div class='col-md-6'><h3>"+item.title+"</h3><p class='well'>"+item.description+"</p><div class='row top-buffer'><div class='col-md-5 col-md-offset-1'><img src='img/link_icon.png' class='img-rounded' width='20px' height='20px'/><a href='www.link.com'>liNK</a></div><div class='col-md-6'><button class='btn btn-primary pull-right' id='btn'>Read More</button></a> </div></div></div>")
});
}
};
});
});
$(文档).ready(函数(){
$.ajax({
url:'json.php',
键入:“post”,
数据:{get_param:'value'},
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
var obj=jQuery.parseJSON(数据);
如果(目标成功){
$.each(obj,函数(索引,项){
如果('success'!=索引){
$(“#输出”).append(“+item.title+”“+item.description+”
阅读更多信息)
});
}
};
});
});
有人注意到这段代码有什么错误吗?json的格式是正确的?
数据已经是jQuery转换的对象。您不需要解析
$(函数(){
$.ajax({
url:'json.php',
键入:“POST”,
数据:{get_param:'value'},
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
//用控制台检查
控制台日志(数据);
$。每个(数据、功能(索引、项目){
$('#output').append(“+item.title+”“+item.description+”
阅读更多信息”);
});
}
});
});
您的json文件中没有success
,因此不需要检查
if (obj.success) {
您不需要以下行,因为数据已经是json了
var obj = jQuery.parseJSON(data);
js代码
$(document).ready(function () {
$.ajax({
url: 'json.php',
type: 'post',
data: {get_param: 'value'},
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
$.each(data, function (index, item) {
$('#output').append("<div class='col-md-6' ><img class='img-rounded' src=" + item.image + "alt='MyImage' width='550px' height='240px'></div><div class='col-md-6'><h3>" + item.title + "</h3><p class='well'>" + item.description + "</p><div class='row top-buffer'><div class='col-md-5 col-md-offset-1'><img src='img/link_icon.png' class='img-rounded' width='20px' height='20px'/><a href='www.link.com'>liNK</a></div><div class='col-md-6'><button class='btn btn-primary pull-right' id='btn'>Read More</button></a> </div></div></div>")
});
}
});
});
$(文档).ready(函数(){
$.ajax({
url:'json.php',
键入:“post”,
数据:{get_param:'value'},
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
$。每个(数据、功能(索引、项目){
$(“#输出”).append(“+item.title+”“+item.description+”
阅读更多信息)
});
}
});
});
如果您定义了数据类型:'json'
那么就不需要jQuery.parseJSON(数据);
检查这个
$(document).ready(function () {
$.ajax({
url: 'json.php',
type: 'post',
data: {get_param: 'value'},
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
$.each(data, function (index, item) {
$('#output').append("<div class='col-md-6' ><img class='img-rounded' src=" + item.image + "alt='MyImage' width='550px' height='240px'></div><div class='col-md-6'><h3>" + item.title + "</h3><p class='well'>" + item.description + "</p><div class='row top-buffer'><div class='col-md-5 col-md-offset-1'><img src='img/link_icon.png' class='img-rounded' width='20px' height='20px'/><a href='www.link.com'>liNK</a></div><div class='col-md-6'><button class='btn btn-primary pull-right' id='btn'>Read More</button></a> </div></div></div>")
});
}
});
});