你好,我执行$('tbody').html(data.table_data);
时有一个json数组
在我的ajax中,我得到以下回报
[{"id":28,"fname":"tester","lname":"testlast","phone":"00000000","email":"test@email.com","address":"Tester 10","country":"TesterCountry","city":"TesterTown","bday":"2222-02-22","username":"admin","password":"1234","access":"Admin","created_at":"2018-10-13 16:34:22","updated_at":"2018-10-14 12:50:26"},{"id":29,"fname":"tester2","lname":"tester2last","phone":"11000000","email":"email2@test2.com","address":"Testaria 22","country":"TEsterio","city":"Testeriontiown","bday":"8812-09-08","username":"admin","password":"1234","access":"admin","created_at":"2018-10-14 09:16:50","updated_at":"2018-10-14 12:51:26"}][{"id":28,"fname":"tester","lname":"testlast","phone":"00000000","email":"test@email.com","address":"Tester 10","country":"TesterCountry","city":"TesterTown","bday":"2222-02-22","username":"admin","password":"1234","access":"Admin","created_at":"2018-10-13 16:34:22","updated_at":"2018-10-14 12:50:26"},{"id":29,"fname":"tester2","lname":"tester2last","phone":"11000000","email":"email2@test2.com","address":"Testaria 22","country":"TEsterio","city":"Testeriontiown","bday":"8812-09-08","username":"admin","password":"1234","access":"admin","created_at":"2018-10-14 09:16:50","updated_at":"2018-10-14 12:51:26"}]
并使用data.total_data从我拥有的所有记录总数中获取数字
好,所以每当我尝试输出此数据* id:28,fname:tester等时,我都无法定义。
代码如下。
function fetch_customer_data(query = '')
{
$.ajax({
url:"{{ route('index.action') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
var client_data = '';
$.each(data,function (key,value) {
client_data += '<tr>';
client_data += '<td>' +value.id +'</td>';
client_data += '<td>' +value.fname+'</td>';
client_data += '<td>' +value.lname+'</td>';
client_data += '<td>' +value.email+'</td>';
client_data += '<td>' +value.phone+'</td>';
client_data += '<td>' +value.address+'</td>';
client_data += '<td>' +value.country+'</td>';
client_data += '<td>' +value.city+'</td>';
client_data += '<td>' +value.bday+'</td>';
client_data += '<td>' +value.username+'</td>';
client_data += '</tr>';
})
$('tbody').html(cleint_data);
$('#total_records').text(data.total_data);
}
})
}
编辑:我不知道如何为您提供实际的答复,所以我会做第二件事。病给你看我是怎么产生的。也许这就是我的问题所在
function action(Request $request)
{
if ($request->ajax()) {
$output = '';
$query = $request->get('query');
if ($query != '') {
$data = DB::table('users')->
where('fname', 'like', '%' . $query . '%')
->orWhere('lname', 'like', '%' . $query . '%')
->orWhere('email', 'like', '%' . $query . '%')
->orWhere('phone', 'like', '%' . $query . '%')
->orWhere('address', 'like', '%' . $query . '%')
->orWhere('country', 'like', '%' . $query . '%')
->orWhere('city', 'like', '%' . $query . '%')
->orWhere('bday', 'like', '%' . $query . '%')
->orWhere('username', 'like', '%' . $query . '%')
->orWhere('access', 'like', '%' . $query . '%')
->orderBy('id', 'desc')
->get();
} else {
$data = DB::table('users')
->orderBy('id', 'asc')
->get();
}
$total_row = $data->count();
if ($total_row > 0) {
foreach ($data as $row) {
$output .= $data;
}
} else {
$output = '
<tr>
<td align="center" colspan="13">No Data Found</td>
</tr>
';
}
$datas = array(
'table_data' => $output,
'total_data' => $total_row
);
echo json_encode($datas);
}
}
edit这是json输出
{table_data: "[{"id":28,"fname":"tester","lname":"testlast","pho…14 09:16:50","updated_at":"2018-10-14 12:51:26"}]", total_data: 2}
table_data: "[{"id":28,"fname":"tester","lname":"testlast","phone":"00000000","email":"test@email.com","address":"Tester 10","country":"TesterCountry","city":"TesterTown","bday":"2222-02-22","username":"admin","password":"1234","access":"Admin","created_at":"2018-10-13 16:34:22","updated_at":"2018-10-14 12:50:26"},{"id":29,"fname":"tester2","lname":"tester2last","phone":"11000000","email":"email2@test2.com","address":"Testaria 22","country":"TEsterio","city":"Testeriontiown","bday":"8812-09-08","username":"admin","password":"1234","access":"admin","created_at":"2018-10-14 09:16:50","updated_at":"2018-10-14 12:51:26"}][{"id":28,"fname":"tester","lname":"testlast","phone":"00000000","email":"test@email.com","address":"Tester 10","country":"TesterCountry","city":"TesterTown","bday":"2222-02-22","username":"admin","password":"1234","access":"Admin","created_at":"2018-10-13 16:34:22","updated_at":"2018-10-14 12:50:26"},{"id":29,"fname":"tester2","lname":"tester2last","phone":"11000000","email":"email2@test2.com","address":"Testaria 22","country":"TEsterio","city":"Testeriontiown","bday":"8812-09-08","username":"admin","password":"1234","access":"admin","created_at":"2018-10-14 09:16:50","updated_at":"2018-10-14 12:51:26"}]"
total_data: 2
答案 0 :(得分:1)
根据我的观察,您有两个错误,
1)您的Json结果格式不正确。
2)您要添加$('tbody').html(cleint_data);
,在此您拼写了单词client_data
$(document).ready(function(){
// Your json result has an error, So I corrected it and got the output. It has some unrecognized square brackets in the middle with one missing comma. check again
var yourData = jQuery.parseJSON('[{"id":"28","fname":"tester","lname":"testlast","phone":"00000000","email":"test@email.com","address":"Tester 10","country":"TesterCountry","city":"TesterTown","bday":"2222-02-22","username":"admin","password":"1234","access":"Admin","created_at":"2018-10-13 16:34:22","updated_at":"2018-10-14 12:50:26"},{"id":"29","fname":"tester2","lname":"tester2last","phone":"11000000","email":"email2@test2.com","address":"Testaria 22","country":"TEsterio","city":"Testeriontiown","bday":"8812-09-08","username":"admin","password":"1234","access":"admin","created_at":"2018-10-14 09:16:50","updated_at":"2018-10-14 12:51:26"},{"id":"28","fname":"tester","lname":"testlast","phone":"00000000","email":"test@email.com","address":"Tester 10","country":"TesterCountry","city":"TesterTown","bday":"2222-02-22","username":"admin","password":"1234","access":"Admin","created_at":"2018-10-13 16:34:22","updated_at":"2018-10-14 12:50:26"},{"id":"29","fname":"tester2","lname":"tester2last","phone":"11000000","email":"email2@test2.com","address":"Testaria 22","country":"TEsterio","city":"Testeriontiown","bday":"8812-09-08","username":"admin","password":"1234","access":"admin","created_at":"2018-10-14 09:16:50","updated_at":"2018-10-14 12:51:26"}]');
var client_data = '';
$.each(yourData,function (key,value) {
client_data += '<tr>';
client_data += '<td>' +value.id +'</td>';
client_data += '<td>' +value.fname+'</td>';
client_data += '<td>' +value.lname+'</td>';
client_data += '<td>' +value.email+'</td>';
client_data += '<td>' +value.phone+'</td>';
client_data += '<td>' +value.address+'</td>';
client_data += '<td>' +value.country+'</td>';
client_data += '<td>' +value.city+'</td>';
client_data += '<td>' +value.bday+'</td>';
client_data += '<td>' +value.username+'</td>';
client_data += '</tr>';
})
$('tbody').html(client_data);
// $('#total_records').text(data.total_data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tbody>
</tbody>
</table>
答案 1 :(得分:0)
好的,因此我想首先感谢SilentCode的帮助,如果没有您,我永远都不会发现我的Json字符串无效,您共享的网站http://json2csharp.com/向我展示了错误所在我的弦是。所以我发现创建错误是在我的控制器内部。
if ($total_row > 0) {
foreach ($data as $row) {
$output .= $data;
}
在这里$output .= $data;
和DOT EQUALS一样,每当字符串沿每行向下移动时,它将一次又一次地添加相同的字符串。 (因此,如果我有100条记录,则该字符串可能会很大)
要解决该问题,我所需要做的就是删除foreach循环(当Iw在控制器内生成html时,不需要在那儿放它,“不要照我说的那样做”),并替换{ {1}}到.=
=
在我看来,我只是保留了原来的格式,只是将 if ($total_row > 0) {
$output = $data;
}
更改为$.each(data,function...
,一切开始正常运行
$.each(data.table_data,function...
总而言之,我对数据库中的每个记录都重复了我的Json,并试图从对象的错误部分(数据)而不是(data.data_table)中拉出