追加函数()在 PHP 响应的 jQuery Ajax 中不起作用
Append function() is not working in jQuery Ajax of PHP response
我正在通过 PHP 获取数组,我想在 table 中追加,但我试图从过去 2 天开始制作它,并使用了 Whosebug 上可用的所有功能,但它不是在职的...
请开发人员帮助我并教我如何在 table 中附加输出数据,如下所示 -
PHP代码:-
$result = $stmt->fetchAll();
foreach($result as $data => $value) {
//$QL QUERY HERE
$data = array('name' => $value["name"], 'amount' => $amount, 'invoice' => $Invoice, 'response' => '200');
echo json_encode($data);
}
exit();
我的 PHP 回复是:-
{"name":"AMAZON_FBA","amount":"1","invoice":"25","response":"200"}
{"name":"AMAZON_IN","amount":"12","invoice":"22","response":"200"}
{"name":"FLIPKART","amount":"42","invoice":"08","response":"200"}
{"name":"PAYTM","amount":"36","invoice":"03","response":"200"}
{"name":"Replacement","amount":"0","invoice":"17","response":"200"}
Ajax:-
$.ajax({
.
.
success: function (data) {
var my_orders = $("table#salesReturnTB > tbody.segment_sales_return");
$.each(data, function(i, order){
my_orders.append("<tr>");
my_orders.append("<td>" + data[i].order.name + "</td>");
my_orders.append("<td>" + data[i].order.invoice + "</td>");
my_orders.append("<td>" + data[i].order.amount + "</td>");
my_orders.append("<td>" + data[i].order.response + "</td>");
my_orders.append("</tr>");
});
});
});
在 PHP 中您需要定义一个 $list
变量(因为您已经在循环中使用 $data
名称作为输入参数),然后移动 echo
在循环外。否则它会回显每个单独的数据项,而不是创建一个连贯的 JSON 数组。两端没有 [..]
的单个项目列表无效 JSON,因此 JavaScript 无法读取它。
$result = $stmt->fetchAll();
$list = array();
foreach($result as $data => $value)
{
$list = array('name' => $value["name"], 'amount' => $amount, 'invoice' => $Invoice, 'response' => '200');
}
header("Content-Type: application/json");
echo json_encode($list);
exit();
而且,在 JavaScript/jQuery 中,您有一个类似的问题,您使用 data
名称来表示两个不同的事物 - 项目列表和循环中的单个项目.
这应该会更好:
$.each(data, function(i, item) {
my_orders.append("<tr>");
my_orders.append("<td>" + item.name + "</td>");
my_orders.append("<td>" + item.invoice + "</td>");
my_orders.append("<td>" + item.amount + "</td>");
my_orders.append("<td>" + item.response + "</td>");
my_orders.append("</tr>");
});
我正在通过 PHP 获取数组,我想在 table 中追加,但我试图从过去 2 天开始制作它,并使用了 Whosebug 上可用的所有功能,但它不是在职的... 请开发人员帮助我并教我如何在 table 中附加输出数据,如下所示 -
PHP代码:-
$result = $stmt->fetchAll();
foreach($result as $data => $value) {
//$QL QUERY HERE
$data = array('name' => $value["name"], 'amount' => $amount, 'invoice' => $Invoice, 'response' => '200');
echo json_encode($data);
}
exit();
我的 PHP 回复是:-
{"name":"AMAZON_FBA","amount":"1","invoice":"25","response":"200"}
{"name":"AMAZON_IN","amount":"12","invoice":"22","response":"200"}
{"name":"FLIPKART","amount":"42","invoice":"08","response":"200"}
{"name":"PAYTM","amount":"36","invoice":"03","response":"200"}
{"name":"Replacement","amount":"0","invoice":"17","response":"200"}
Ajax:-
$.ajax({
.
.
success: function (data) {
var my_orders = $("table#salesReturnTB > tbody.segment_sales_return");
$.each(data, function(i, order){
my_orders.append("<tr>");
my_orders.append("<td>" + data[i].order.name + "</td>");
my_orders.append("<td>" + data[i].order.invoice + "</td>");
my_orders.append("<td>" + data[i].order.amount + "</td>");
my_orders.append("<td>" + data[i].order.response + "</td>");
my_orders.append("</tr>");
});
});
});
在 PHP 中您需要定义一个 $list
变量(因为您已经在循环中使用 $data
名称作为输入参数),然后移动 echo
在循环外。否则它会回显每个单独的数据项,而不是创建一个连贯的 JSON 数组。两端没有 [..]
的单个项目列表无效 JSON,因此 JavaScript 无法读取它。
$result = $stmt->fetchAll();
$list = array();
foreach($result as $data => $value)
{
$list = array('name' => $value["name"], 'amount' => $amount, 'invoice' => $Invoice, 'response' => '200');
}
header("Content-Type: application/json");
echo json_encode($list);
exit();
而且,在 JavaScript/jQuery 中,您有一个类似的问题,您使用 data
名称来表示两个不同的事物 - 项目列表和循环中的单个项目.
这应该会更好:
$.each(data, function(i, item) {
my_orders.append("<tr>");
my_orders.append("<td>" + item.name + "</td>");
my_orders.append("<td>" + item.invoice + "</td>");
my_orders.append("<td>" + item.amount + "</td>");
my_orders.append("<td>" + item.response + "</td>");
my_orders.append("</tr>");
});