Append function()在PHP响应的jQuery Ajax中不起作用 [英] Append function() is not working in jQuery Ajax of PHP response

查看:92
本文介绍了Append function()在PHP响应的jQuery Ajax中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过PHP获取数组,我想在表中追加,但是我尝试从最近2天开始使用它,并使用了stackoverflow上可用的所有功能,但是它不起作用... 请开发人员,帮助我,并教我如何在下表中附加输出数据-

I am getting the array through PHP and I want to append in in table but I tried to make it from last 2 days and uses all the function which available on stackoverflow but it not working... Please developers , help me and teach me how i append out output data in table as below -

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无法读取.

in the PHP you need to define a $list variable (because you've already used the $data name as an input parameter in your loop), and move the echo outside the loop. Otherwise it echoes each individual data item, rather than creating a coherent JSON array. A list of individual items without the [..] at each end isn't valid JSON, so JavaScript can't read it.

$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名称表示两个不同的东西-项目列表和循环中的单个项目.

And, in the JavaScript/jQuery, you have a similar problem where you're using the data name to represent two different things - the list of items, and an individual item within the loop.

这应该更好地工作:

$.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>");
});

这篇关于Append function()在PHP响应的jQuery Ajax中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆