简体   繁体   English

如何以模式方式将Ajax数据插入表

[英]How to Insert ajax data into table in modal

This is the ajax code that takes the data in json and creates a table with the data. 这是一个ajax代码,它将json中的数据并使用该数据创建一个表。 But I'm getting this error: 但我收到此错误:

parsererror parsererror

 $('#loadddx').click(function(e) { e.preventDefault(); $.ajax({ url: $(this).attr('href'), // Example: ajax.php?id= dataType: 'json', success: function(resp) { var trHTML = ''; $.each(resp, function(i, userData) { for (i = 0; i < resp.userData.length; i++) { trHTML += '<tr><td>' + resp.userData[i].pais + '</td><td>' + resp.userData[i].data + '</td><td>' + resp.userData[i].origem + '</td><td>' + resp.userData[i].ip + '</td><td>' + resp.userData[i].isp + '</td><td>' + resp.userData[i].browser + '</td><td>' + resp.userData[i].os + resp.userData[i].newid + '</td></tr>'; } }); $('#result').append(trHTML); console.log(resp); $("#showDataa").modal("show"); }, error: function(xhr, status) { console.log(status); } }); }); 

Here is the .php file that returns the data 这是返回数据的.php文件

 $res = []; while ($stmt - > fetch()) { $res[] = array("success" => true, "pais" => $pais, "data" => $data, "origem" => $origem, "ip" => $ip, "isp" => $isp, "browser" => $browser, "os" => $os, "newid" => $newid); echo json_encode($res); } 

Can anyone help me? 谁能帮我?

Try updating your PHP as follows: 尝试如下更新您的PHP:

$stmt->fetch();
echo json_encode(array("success" => true, "pais" => $pais, "data" => $data, "origem" => $origem, "ip" => $ip, "isp" => $isp, "browser" => $browser, "os" => $os, "newid" => $newid));

Not sure what the while loop was for? 不知道while循环是干什么用的? If the values within the loop are globals being updated by $stmt->fetch() each iteration and you need multiple iterations try the following: 如果循环中的值是通过$stmt->fetch()每次迭代更新的全局变量,并且您需要多次迭代,请尝试以下操作:

$res = [];
while($stmt->fetch()) $res[] = array("success" => true, "pais" => $pais, "data" => $data, "origem" => $origem, "ip" => $ip, "isp" => $isp, "browser" => $browser, "os" => $os, "newid" => $newid);
echo json_encode($res);

UPDATE UPDATE

Update your JS as follows, assuming you used the first PHP example I gave you. 假设您使用了我给您的第一个PHP示例,请按照以下步骤更新JS。 Not sure the way you are appending your table rows is the best, but this should resolve your issue. 不确定添加表行的方式是否最好,但这应该可以解决您的问题。 If it doesn't please provide the console.log output for resp: 如果没有,请提供console.log输出以供响应:

 $('#loadddx').click(function(e) { e.preventDefault(); $.ajax({ url: $(this).attr('href'), // Example: ajax.php?id= dataType: 'json', success: function(resp) { console.log(resp); $('#result').append( '<tr><td>' + resp.pais + '</td><td>' + resp.data + '</td><td>' + resp.origem + '</td><td>' + resp.ip + '</td><td>' + resp.isp + '</td><td>' + resp.browser + '</td><td>' + resp.os + resp.newid + '</td></tr>'; } $("#showDataa").modal("show"); }, error: function(xhr, status) { console.log(status); } }); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM