简体   繁体   English

使用AJAX响应将JSON对象解析并转换为html表

[英]Parse and transform JSON Objects to an html table using AJAX Response

I am using SYMFONY 3 and i want to parse a received JSON from controller using Ajax , the issue there that the JSON can't be read properly and returns undefined this is my code below : 我正在使用SYMFONY 3,我想使用Ajax解析从控制器接收到的JSON,那里的JSON无法正确读取并返回undefined的问题是我的以下代码:

Controller : 控制器:

 $em = $this->getDoctrine()->getManager();

            $RAW_QUERY = 'SELECT id,DO_Date,DL_Design,DL_Qte,DL_MontantHT,DL_MontantTTC FROM `facture_ligne` WHERE facture_id=:id';

            $statement = $em->getConnection()->prepare($RAW_QUERY);
            // Set parameters
            $statement->bindValue('id', $id);
            $statement->execute();

            $facture = $statement->fetchAll();

            $serializer = $this->container->get('jms_serializer');
            $reports = $serializer->serialize($facture,'json');
            return new Response($reports); 

My script in a the twig file : 我的脚本在树枝文件中:

function detailfacture{{ fact.id }} (id) {

    var z= new XMLHttpRequest();
    z.open("get","{{ path('Ajaxonify',{'id':fact.id}) }})",true);
    z.send()
    z.onreadystatechange = function result () {
        var json=z.responseText;
        if(json!="")
        {
            alert(json);

            var tr;
            for (var i = 0; i < json.length; i++) {
                tr = $('<tr/>');
                tr.append("<td>" + json[1].DL_MontantHT + "</td>");
                tr.append("<td>" + json[1].DO_Date + "</td>");
                tr.append("<td>" + json[1].DL_Qte + "</td>");
                $('#tb').append(tr);
            }  
        }
        else alert("helo");    
    }
}

And this some screen shots of the results : 这是结果的一些屏幕截图: 在此处输入图片说明 在此处输入图片说明

I just tested with a static JSON and it works fine 我刚刚用静态JSON测试过,效果很好

This is what 这是什么

console.log(data);
console.log(json);

returns 退货

在此处输入图片说明

If you just want to read the JSON, there is a lot of plugins Like JSON Formatter . 如果您只想阅读JSON,则有很多插件,例如JSON Formatter To use it, just output the JSON and the plugin will identify and activate, formatting JSON for easy reading. 要使用它,只需输出JSON,插件便会识别并激活,格式化JSON以便于阅读。

Another answer is pretty print JSON: 另一个答案是漂亮的打印JSON:

PHP: $echo "<pre>".json_encode($data, JSON_PRETTY_PRINT)."</pre>"; PHP: $echo "<pre>".json_encode($data, JSON_PRETTY_PRINT)."</pre>";

JS: document.write(JSON.stringify(data, null,4)); JS: document.write(JSON.stringify(data, null,4));

You need to parse the JSON into a JavaScript object before using it, like this: 您需要先将JSON解析为JavaScript对象,如下所示:

var json = JSON.parse(json);

Insert this line after alert(json) but before the for loop, so your script looks like this: alert(json)但在for循环之前插入此行,因此您的脚本如下所示:

function detailfacture{{ fact.id }} (id) {

    var z= new XMLHttpRequest();
    z.open("get","{{ path('Ajaxonify',{'id':fact.id}) }})",true);
    z.send()
    z.onreadystatechange = function result () {
        var json=z.responseText;
        if(json!="")
        {
            alert(json);
            json = JSON.parse(json);

            var tr;
            for (var i = 0; i < json.length; i++) {
                tr = $('<tr/>');
                tr.append("<td>" + json[i].DL_MontantHT + "</td>");
                tr.append("<td>" + json[i].DO_Date + "</td>");
                tr.append("<td>" + json[i].DL_Qte + "</td>");
                $('#tb').append(tr);
            }  
        }
        else alert("helo");    
    }
}

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

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