在html div中显示json结果(来自php)(使用Ajax和jQuery)
我从一个php文件中获得json结果,我想使用ajax和jQuery将它们附加到另一个php文件中的特定div部分 json结果:在html div中显示json结果(来自php)(使用Ajax和jQuery),jquery,json,ajax,Jquery,Json,Ajax,我从一个php文件中获得json结果,我想使用ajax和jQuery将它们附加到另一个php文件中的特定div部分 json结果: [ { "title": "Welcome!", "description": "The world has changed dramatically..", "image": "img/article1.jpg" }, { "title": "Welcome 2!", "description": "It is time f
[
{
"title": "Welcome!",
"description": "The world has changed dramatically..",
"image": "img/article1.jpg"
},
{
"title": "Welcome 2!",
"description": "It is time for a new start..",
"image": "img/article2.jpg"
}
]
我使用的jQuery是:
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: 'ajax_json.php',
type: 'post',
data: {tag: 'getData'},
dataType: 'json',
success: function(data){
if(data.success){
$.each(data, function(index, record){
if($.is_numeric(index)){
var row =
$("#output").append('<div class="article"><div>' + item.title + '</div><div style="width:200px;height:100px;background-image: url(' + item.image + ')"></div></div>');
});
}
})
}
}
});
});
$(文档).ready(函数(){
$.ajax({
url:'ajax_json.php',
键入:“post”,
数据:{tag:'getData'},
数据类型:“json”,
成功:功能(数据){
if(data.success){
$。每个(数据、函数(索引、记录){
如果($是数字(索引)){
变量行=
$(“#输出”).append(“”+item.title+“”);
});
}
})
}
}
});
});
分区组:
<div class="row top-buffer" id="output">
<div class="col-md-6" >
<img class="img-rounded" src="json image" alt="MyImage" width="550px" height="240px">
</div>
<div class="col-md-6">
<h3>json title</h3>
<p class="well">json description<p>
</div>
json标题
json描述
您可能正在从服务器获取json作为字符串
尝试在if条件之前使用,并使用articleData进行处理-
var articleData = JSON.parse(data);
此外,它应该是record.title,而不是item.title。您可能从服务器获取json作为字符串 尝试在if条件之前使用,并使用articleData进行处理-
var articleData = JSON.parse(data);
此外,它应该是record.title,而不是item.title。丢失了一些小错误 固定html代码:
<div class="row top-buffer" id="output">
<div class="col-md-6" >
<img class="img-rounded" src="json image" alt="MyImage" width="550px" height="240px">
</div>
<div class="col-md-6">
<h3>json title</h3>
<p class="well">json description<p>
</div>
</div>
if (data.success){
$.each(data, function (index, item) {
if ('success'!= index){
$("#output").append('<div class="article">1<div>' + item.title + '</div><div style="width:200px;height:100px;background-image: url(' + item.image + ')"></div></div>');
}
});
};
json标题
json描述
固定js代码:
<div class="row top-buffer" id="output">
<div class="col-md-6" >
<img class="img-rounded" src="json image" alt="MyImage" width="550px" height="240px">
</div>
<div class="col-md-6">
<h3>json title</h3>
<p class="well">json description<p>
</div>
</div>
if (data.success){
$.each(data, function (index, item) {
if ('success'!= index){
$("#output").append('<div class="article">1<div>' + item.title + '</div><div style="width:200px;height:100px;background-image: url(' + item.image + ')"></div></div>');
}
});
};
if(data.success){
$。每个(数据、功能(索引、项目){
如果('success'!=索引){
$(“#输出”).append('1'+item.title+”);
}
});
};
主要错误在于:
$.each(data, function(index, record){
//item is in record var but then you are trying to use item var
$("#output").append('<div class="article">1<div>' + item.title + '</div><div style="width:200px;height:100px;background-image: url(' + item.image + ')"></div></div>');
$。每个(数据、函数(索引、记录){
//项在记录变量中,但您正在尝试使用项变量
$(“#输出”).append('1'+item.title+”);
希望它能帮助你克服一些小错误 固定html代码:
<div class="row top-buffer" id="output">
<div class="col-md-6" >
<img class="img-rounded" src="json image" alt="MyImage" width="550px" height="240px">
</div>
<div class="col-md-6">
<h3>json title</h3>
<p class="well">json description<p>
</div>
</div>
if (data.success){
$.each(data, function (index, item) {
if ('success'!= index){
$("#output").append('<div class="article">1<div>' + item.title + '</div><div style="width:200px;height:100px;background-image: url(' + item.image + ')"></div></div>');
}
});
};
json标题
json描述
固定js代码:
<div class="row top-buffer" id="output">
<div class="col-md-6" >
<img class="img-rounded" src="json image" alt="MyImage" width="550px" height="240px">
</div>
<div class="col-md-6">
<h3>json title</h3>
<p class="well">json description<p>
</div>
</div>
if (data.success){
$.each(data, function (index, item) {
if ('success'!= index){
$("#output").append('<div class="article">1<div>' + item.title + '</div><div style="width:200px;height:100px;background-image: url(' + item.image + ')"></div></div>');
}
});
};
if(data.success){
$。每个(数据、功能(索引、项目){
如果('success'!=索引){
$(“#输出”).append('1'+item.title+”);
}
});
};
主要错误在于:
$.each(data, function(index, record){
//item is in record var but then you are trying to use item var
$("#output").append('<div class="article">1<div>' + item.title + '</div><div style="width:200px;height:100px;background-image: url(' + item.image + ')"></div></div>');
$。每个(数据、函数(索引、记录){
//项在记录变量中,但您正在尝试使用项变量
$(“#输出”).append('1'+item.title+”);
希望它能有所帮助=)Html应该是
<div class="row top-buffer" id="output">
</div>
var json_ = {
"0": {
"title": "Welcome!",
"description": "The world has changed dramatically..",
"image": "img\/article1.jpg"
},
"1": {
"title": "Welcome 2!",
"description": "It is time for a new start..",
"image": "img\/article2.jpg"
},
"success": "true"
};
if (json_.success){
$.each(json_, 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>");
}
});
};
你的JS应该是
<div class="row top-buffer" id="output">
</div>
var json_ = {
"0": {
"title": "Welcome!",
"description": "The world has changed dramatically..",
"image": "img\/article1.jpg"
},
"1": {
"title": "Welcome 2!",
"description": "It is time for a new start..",
"image": "img\/article2.jpg"
},
"success": "true"
};
if (json_.success){
$.each(json_, 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>");
}
});
};
var-json\uuux={
"0": {
“标题”:“欢迎!”,
“描述”:“世界发生了巨大变化……”,
“图像”:“img\/article1.jpg”
},
"1": {
“标题”:“欢迎2!”,
“说明”:“是时候重新开始了…”,
“图像”:“img\/article2.jpg”
},
“成功”:“真实”
};
if(json_u2;.success){
$.each(json),函数(索引,项){
如果('success'!=索引){
$('#output')。追加(“+item.title+”“+item.description+””);
}
});
};
检查结果如下:
您正在使用外部php文件,因此您的代码应该是:
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: 'ajax_json.php',
type: 'post',
data: {tag: 'getData'},
dataType: 'json',
success: function(data){
if(data.success){
$.each(data, 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>");
});
}
};
});
});
$(文档).ready(函数(){
$.ajax({
url:'ajax_json.php',
键入:“post”,
数据:{tag:'getData'},
数据类型:“json”,
成功:功能(数据){
if(data.success){
$。每个(数据、功能(索引、项目){
如果('success'!=索引){
$('#output')。追加(“+item.title+”“+item.description+””);
});
}
};
});
});
Html应该是
<div class="row top-buffer" id="output">
</div>
var json_ = {
"0": {
"title": "Welcome!",
"description": "The world has changed dramatically..",
"image": "img\/article1.jpg"
},
"1": {
"title": "Welcome 2!",
"description": "It is time for a new start..",
"image": "img\/article2.jpg"
},
"success": "true"
};
if (json_.success){
$.each(json_, 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>");
}
});
};
你的JS应该是
<div class="row top-buffer" id="output">
</div>
var json_ = {
"0": {
"title": "Welcome!",
"description": "The world has changed dramatically..",
"image": "img\/article1.jpg"
},
"1": {
"title": "Welcome 2!",
"description": "It is time for a new start..",
"image": "img\/article2.jpg"
},
"success": "true"
};
if (json_.success){
$.each(json_, 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>");
}
});
};
var-json\uuux={
"0": {
“标题”:“欢迎!”,
“描述”:“世界发生了巨大变化……”,
“图像”:“img\/article1.jpg”
},
"1": {
“标题”:“欢迎2!”,
“说明”:“是时候重新开始了…”,
“图像”:“img\/article2.jpg”
},
“成功”:“真实”
};
if(json_u2;.success){
$.each(json),函数(索引,项){
如果('success'!=索引){
$('#output')。追加(“+item.title+”“+item.description+””);
}
});
};
检查结果如下:
您正在使用外部php文件,因此您的代码应该是:
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: 'ajax_json.php',
type: 'post',
data: {tag: 'getData'},
dataType: 'json',
success: function(data){
if(data.success){
$.each(data, 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>");
});
}
};
});
});
$(文档).ready(函数(){
$.ajax({
url:'ajax_json.php',
键入:“post”,
数据:{tag:'getData'},
数据类型:“json”,
成功:功能(数据){
if(data.success){
$。每个(数据、功能(索引、项目){
如果('success'!=索引){
$('#output')。追加(“+item.title+”“+item.description+””);
});
}
};
});
});
您的class=“output”在哪里在htmlI中,我刚刚更新了我的代码。这是我第一次使用AJAX,所以现在对我来说有点棘手。你在一个简单的AJAX中犯了这么多错误,你正在添加项。title
项在哪里。如果要全部重写。问?对不起,我不能理解你。代码中有什么错误?好的,复制所有内部#output div并粘贴它inside.append现在您将json图像、json标题和json描述替换为item.title、item.description等。您应该在#output.内部追加内容,并删除var行
class=“output”的位置在htmlI中,我刚刚更新了我的代码。这是我第一次使用AJAX,所以现在对我来说有点棘手。你在一个简单的AJAX中犯了这么多错误,你正在添加项。title
项在哪里。如果要全部重写。问?对不起,我不能理解你。代码中有什么错误?好的,复制所有内部#output div并粘贴它inside.app