Javascript 如何执行这个数组(jqueryajax)?
我有一个PHP脚本,它返回一个包含4条记录的对象:Javascript 如何执行这个数组(jqueryajax)?,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我有一个PHP脚本,它返回一个包含4条记录的对象: $obj = array( 'id'=>$id, 'idCliente'=>$idCliente, 'arquivo'=>$arquivo, 'titulo'=>$titulo, 'descricao'=>$descricao, 'data' => $datacadastro ); 我需要在HTML中逐个显示记录。我是jQuery的新手,我试着写这段代
$obj = array(
'id'=>$id,
'idCliente'=>$idCliente,
'arquivo'=>$arquivo,
'titulo'=>$titulo,
'descricao'=>$descricao,
'data' => $datacadastro
);
我需要在HTML中逐个显示记录。我是jQuery的新手,我试着写这段代码3天,所以我决定问一下
<div>
ID:
IDCliente:
Arquivo:
Titulo:
Descricao:
Data:
</div>
<div>
ID:
IDCliente:
Arquivo:
Titulo:
Descricao:
Data:
</div>
<div>
ID:
IDCliente:
Arquivo:
Titulo:
Descricao:
Data:
</div>
<div>
ID:
IDCliente:
Arquivo:
Titulo:
Descricao:
Data:
</div>
身份证件:
IDclient:
阿奎沃:
提图洛:
描述:
数据:
身份证件:
IDclient:
阿奎沃:
提图洛:
描述:
数据:
身份证件:
IDclient:
阿奎沃:
提图洛:
描述:
数据:
身份证件:
IDclient:
阿奎沃:
提图洛:
描述:
数据:
如何进行?我有:
$.ajax ({ type: "POST",
dataType : "json",
cache: false,
url: "<?php bloginfo('template_url'); ?>/scripts/php_functions.php",
data: {idClienteDocumentos: idClienteDocumentos, ajax: "true"},
success: function(result){
$.ajax({type:“POST”,
数据类型:“json”,
cache:false,
url:“/scripts/php_functions.php”,
数据:{idClienteDocumentos:idClienteDocumentos,ajax:“true”},
成功:功能(结果){
我有这个代码,但它不工作
var campos = ['arquivo', 'idCliente', 'titulo', 'descricao', 'arquivo', 'data'];
var tabela = $('<div id="tabela"></div>');
resultado.forEach(function (linha) {
var linhaDiv = $('<div></div>');
linha.forEach(function (campo, index) {
$('<div></div>').addClass(campos[index]).text(campo).appendTo(linhaDiv);
});
linhaDiv.appendTo(tabela);
});
var campos=['arquivo'、'idCliente'、'titulo'、'descripao'、'arquivo'、'data'];
变量表=美元(“”);
resultado.forEach(函数(linha){
变量linhaDiv=$('');
linha.forEach(函数(campo,索引){
$('').addClass(campos[index]).text(campo).appendTo(linhaDiv);
});
林哈迪夫。附录(塔贝拉);
});
您的PHP脚本可以是:
$obj = array(
'id'=>$id,
'idCliente'=>$idCliente,
'arquivo'=>$arquivo,
'titulo'=>$titulo,
'descricao'=>$descricao,
'data' => $datacadastro
);
$result = array('results' => $obj);
echo json_encode($result); die;
$.ajax ({ type: "POST",
dataType : "json",
cache: false,
url: "<?php bloginfo('template_url'); ?>/scripts/php_functions.php",
data: {idClienteDocumentos: idClienteDocumentos, ajax: "true"},
success: function(response){
console.log(response.results.id)
$("#main_div").append(response.results.id);
}
});
您的jQuery可以是:
$obj = array(
'id'=>$id,
'idCliente'=>$idCliente,
'arquivo'=>$arquivo,
'titulo'=>$titulo,
'descricao'=>$descricao,
'data' => $datacadastro
);
$result = array('results' => $obj);
echo json_encode($result); die;
$.ajax ({ type: "POST",
dataType : "json",
cache: false,
url: "<?php bloginfo('template_url'); ?>/scripts/php_functions.php",
data: {idClienteDocumentos: idClienteDocumentos, ajax: "true"},
success: function(response){
console.log(response.results.id)
$("#main_div").append(response.results.id);
}
});
Jquery:
$.ajax ({ type: "POST",
dataType : "json",
cache: false,
url: "<?php bloginfo('template_url'); ?>/scripts/php_functions.php",
data: {idClienteDocumentos: idClienteDocumentos, ajax: "true"},
success: function(response){
$.each(response.results, function(i, item) {
console.log(item.id);
//You can append data to HTML div
$("#main_div").append(item.id);
});
}
});
$.ajax({type:“POST”,
数据类型:“json”,
cache:false,
url:“/scripts/php_functions.php”,
数据:{idClienteDocumentos:idClienteDocumentos,ajax:“true”},
成功:功能(响应){
$。每个(响应结果、功能(i、项目){
console.log(item.id);
//您可以将数据附加到HTML div
$(“#main_div”).append(item.id);
});
}
});
您尝试的代码有两个主要问题。首先,linha.forEach
linha
是一个对象,但是forEach
在数组上迭代。您希望for…in
在对象上迭代
其次,您永远不会将tabela
添加到您的文档中。因此,即使其他一切都运行得很好(而且几乎如此),您也永远不会看到结果
var campos = ['arquivo', 'idCliente', 'titulo', 'descricao', 'arquivo', 'data'];
var tabela = $('<div id="tabela"></div>');
resultado.forEach(function (linha) {console.log(linha);
var linhaDiv = $('<div></div>');
for(var prop in linha)
{
$('<div></div>').addClass(''+prop+'').text(prop+': '+linha[prop]).appendTo(linhaDiv);
}
linhaDiv.appendTo(tabela);
tabela.appendTo('html');
var campos=['arquivo'、'idCliente'、'titulo'、'descripao'、'arquivo'、'data'];
变量表=美元(“”);
resultado.forEach(函数(linha){console.log(linha);
变量linhaDiv=$('');
用于(林哈的var道具)
{
$('').addClass(''+prop+'').text(prop+':''+linha[prop]).appendTo(linhaDiv);
}
林哈迪夫。附录(塔贝拉);
tabela.appendTo('html');
现在,唯一的问题是它没有像你的例子中那样大写标签文本。如果你想这样做,我会做得有点不同
var tabela = $('<div id="tabela"></div>');
$.each(resultado, function(){console.log(this);
$(tabela).append("<div>ID:"+this.id+"<br/>IDCliente:"+this.idCliente+"<br/>Arquivo:"+this.arquivo+"<br/>Titulo:"+this.titulo+"<br/>Descricao:"+this.descricao+"<br/>Data:"+this.data+"<br/></div>");
});
tabela.appendTo('html');
var tabela=$('');
$.each(resultado,function(){console.log(this);
$(tabela).append(“ID:+this.ID+”
IDCliente:+this.IDCliente+”
Arquivo:+this.Arquivo+”
Titulo:+this.Titulo+”
descripao:+this.descripao+“
数据:+this.Data+”
);
});
tabela.appendTo('html');
我要说的最后一件事是,您拥有
success:function(result){
然后resultado.forEach
。您应该在这两个位置都有result
,或者在这两个位置都有resultado
。您可以使用.append
等方法将HTML添加到DOM中。您还可以使用在jquery中创建DOM元素。您的意思是说您正在寻找将PHP数组转换为Java的方法脚本对象表示法(或JSON)?像这样的函数会很方便…你能告诉我它是如何实现的吗?谢谢你的帮助,如果我的英语不是那么专业,很抱歉。你的AJAX调用是否返回正确的JSON,你想知道如何创建HTML吗?或者你甚至还没有弄清楚如何从PHP发回JSON吗?只是想说,作为一个新用户+1,不仅仅是为了编写另一个er'how do code'问题并实际发布您的源代码。感谢您的帮助。您没有忘记循环?在控制台中只显示第一个数组对象。非常感谢。这是我的问题,loop.winnnnnn…我在运行您的脚本时遇到了一些困难,因为它缺少php“while”为了动态地获取项目。我研究了代码并实现了您的脚本,最终在您96%的脚本帮助下获得了解决方案。祝贺您,谢谢您。