Jquery:对返回JSON数据的php脚本的Ajax调用
Jquery: Ajax call to a php script returning JSON data
我是JSON的新手。我有一个带有多维数组的php,它最终以JSON格式编码:
<?php
header('Content-Type: application/json');
$lista = array (
'Conoscenti'=>array (
0=>array(
"Nome"=>"John",
"Cognome"=>"Doe",
"Nato"=>array(
"Giorno"=>16,
"Mese"=>"Febbraio",
"Anno"=>1972
)
),
1=>array(
"Nome"=>"Elvis",
"Cognome"=>"Presley",
"Nato"=>array(
"Giorno"=>12,
"Mese"=>"Luglio",
"Anno"=>1984
)
),
2=>array(
"Nome"=>"Mick",
"Cognome"=>"Jagger",
"Nato"=>array(
"Giorno"=>13,
"Mese"=>"Novembre",
"Anno"=>1984
)
)
),
"Amici"=>array(
0=>array(
"Nome"=>"Michael",
"Cognome"=>"Myers",
"Nato"=>array(
"Giorno"=>8,
"Mese"=>"Dicembre",
"Anno"=>1986
)
),
1=>array(
"Nome"=>"Jim",
"Cognome"=>"Fear",
"Nato"=>array(
"Giorno"=>4,
"Mese"=>"Febbraio",
"Anno"=>1985
)
)
)
);
echo json_encode($lista);
?>
我想通过Ajax加载:为此,我编写了一些JQuery代码:
var output ="<ul>";
$.ajax({
url:"lista.php",
dataType:"json"
}).done(function(data) {
$.each(data.Conoscenti, function() {
output +="<li>"+this.Nome+" "+this.Cognome+" è un mio conoscente. È nato il "+this.Nato.Giorno+" "+this.Nato.Mese+" "+this.Nato.Anno+"</li>";
});
});
output += "</ul>";
$("body").html(output);
但是html页面显示为空白,甚至没有错误。分析源代码时,它只显示ul标记,因为它不在ajax调用中。有人知道我该怎么修吗?非常感谢。
问题是因为promise是在请求完成后执行的。这意味着在对返回的JSON进行循环之前,$('body').html(output)
已经运行。
为了解决这个问题,您需要执行所有依赖于done()
处理程序中的响应的代码。试试这个:
$.ajax({
url:"lista.php",
dataType:"json"
}).done(function(data) {
var output = "<ul>";
$.each(data.Conoscenti, function() {
output += "<li>" + this.Nome + " " + this.Cognome + " è un mio conoscente. È nato il " + this.Nato.Giorno + " " + this.Nato.Mese + " " + this.Nato.Anno + "</li>";
});
output += "</ul>";
$("body").html(output);
});
$.ajax({
url:"lista.php",
dataType:"json"
})
.done(function(data) {
var output ="<ul>";
$.each( data.Conoscenti, function() {
output +="<li>"+ this.Nome+" "+this.Cognome+" è un mio conoscente. È nato il "+ this.Nato.Giorno+" "+ this.Nato.Mese+" "+ this.Nato.Anno+"</li>";
});
output += "</ul>";
$("body").html(output);
});
由于变量"out"的打印发生在ajax请求执行之前,因此会出现此问题。您必须将变量的打印放在回调"done"中。
因此,一个好主意是始终有一个分支"完成"和一个分支(也可能是分支"始终"),以便管理请求的成功或失败。
试试这个:
$.ajax({
url:"lista.php",
dataType:"json"
})
.done(function(data) {
var output = "<ul>";
$.each(data.Conoscenti, function() {
output += "<li>" + this.Nome + " " + this.Cognome + " è un mio conoscente. È nato il " + this.Nato.Giorno + " " + this.Nato.Mese + " " + this.Nato.Anno + "</li>";
});
output += "</ul>";
$("body").html(output);
})
.fail(function( jqXHR, textStatus, errorThrown ){
/*manage your error*/
})
.always(function()){
console.log("completed");
}
Daniele,问题是您在done函数中插入了html,请尝试使用以下代码:
var output ="<ul>";
$.ajax({
url:"lista.php",
dataType:"json"
}).done(function(data) {
$.each(data.Conoscenti, function() {
output +="<li>"+this.Nome+" "+this.Cognome+" è un mio conoscente. È nato il "+this.Nato.Giorno+" "+this.Nato.Mese+" "+this.Nato.Anno+"</li>";
});
output += "</ul>";
$("body").html(output);
});
相关文章:
- php脚本中无法识别ajax传递的值
- 使用JS在服务器上运行PHP脚本
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 使用php脚本验证访问者的年龄,并在不刷新的情况下根据结果加载iframe
- jQuery DataTable返回带有数据的PHP脚本
- 通过JavaScript设置的表单字段的值不会作为$_POST的一部分传递给PHP脚本
- 单击Dojo按钮执行PHP脚本
- 将javascript函数传递给.php脚本
- 使用Ajax或JavaScript选择下拉列表选项后,在同一页面上执行PHP脚本
- AJAX和php脚本后添加的表单元素未传输到$_POST
- jquery ajax到php脚本不保存数据
- 有没有一种方法可以生成Braintree令牌,而不必向我的服务器添加PHP脚本
- 如何使用jQuery将所选文件传递到php脚本
- PHP脚本在Lion上通过Ajax运行时会回显自己的内容
- Codeigniter :通过 AJAX 调用 php 脚本时路由不起作用
- 使用 jQuery 从运行 MySQL 的 PHP 脚本返回信息
- 我将如何在将数据发布到 php 脚本的 javascript 脚本中输入变量
- 在 ajax 成功中触发 php 脚本
- 使用同一页面上的按钮访问 php 脚本
- 从 PHP 脚本预缓存第二个图像