我的AJAX请求是这个-
$.ajax({
type: "POST",
url: "ajax/feed-check.php",
dataType: "json",
data: {
server: server,
},
complete: function(data) {
console.log(data);
$('.Agencies').html('<p>'+data[0]+'</p>');
}
})
上面的返回和数组如下
[{"feed_name":"example.zip","feed_time":"2015-10-16 00:00:24","feed_size":"1222","back_office"
:"example4","agencyID":"example2"},{"feed_name":"example2.zip","feed_time":"2015-10-16 08:20:00","feed_size"
:"3145","back_office":"example1","agencyID":"aaa"}]
"
当AJAX请求中完成了完整的功能时,我该如何获得数据?我正试图像这个一样完成它
complete: function(data) {
$('.Agencies').html('<p>'+data[0]+'</p>');
}
但我越来越不确定了,有人能告诉我哪里出了问题吗?我需要把所有的数据都拿出来。
我的PHP脚本-
$whereArray = array(
"$where",
"=",
$_POST['server'],
);
$andArray = array(); //- Blank 'AND' array so that the 'get' call below doesn't fail if no 'ANDs' are passed.
$orArray = array(); //- Blank 'OR' array so that the 'get' call below doesn't fail if no 'ORs' are passed.
$order = array();
$agencyfeed = paddyDB::getInstance("paddy_ms")->get('feed_files', $whereArray, $andArray, $orArray, $order);
//print_r ($agencyfeed->results());
$feeds = [];
foreach ($agencyfeed->results() as $key) {
$feeds[] = $key;
//$key = $feeds['feed_name'];
}
echo json_encode($feeds);
查看了错误的文件,derp。已用相关详细信息更新了帖子
谢谢。
试试这个:
complete: function(data) {
$.each(data, function(i, member)
{
$(".Agencies").html('<p>'+data[i].feed_name+'</p>');
})
}
您是否告知ajax请求需要JSON响应?如果没有,那么它就不会解码JSON,您只会得到一个字符串。使用console.log(data)进行检查,您可能会得到一个字符串。
通过在jQuery.ajax选项中设置dataType: "JSON"
,可以让jQuery自动将JSON转换为对象。
请尝试下面的代码。我使用了$.each函数嵌套来解析json对象。请参考jsfiddle-https://jsfiddle.net/sashant9/npq9efur/2/
**请告诉我,如果你想要的输出不同。
$.ajax({
type: "POST",
url: "ajax/feed-check.php",
dataType: "json",
data: {
server: server,
},
complete: function(data) {
$.each(data , function(index, value){
$.each(value, function(ind,val){
$('.Agencies').append('<p>'+val+'</p>');
});
});
}
});
输出-->
示例.zip
2015-10-16 00:00:24
1222
示例4
示例2
示例2.zip
2015-10-16 08:20:00
3145
示例1
aaa
感谢您的帮助,我现在找到了一个解决方案
success: function(data) {
$.each(data,function(k,v){
var serverCont = '<tr>';
serverCont += '<td>'+v.feed_name+'</td>';
serverCont += '<td>'+v.feed_date+'</td>';
serverCont += '<td>'+v.feed_time+'</td>';
serverCont += '<td>'+v.feed_size+'kb</td>';
serverCont += '<td>'+v.agencyID+'</td>';
serverCont += '</tr>';
thisServer.append(serverCont);
});
},