dqk42179 2014-11-24 10:47
浏览 45

使用JQuery JSON打印MySQL查询结果,而不知道数组键的名称

I am building a facility on a website (using Symfony2) that allows the user to create reports and display them on a screen using AJAX. The reports are effectively SQL statements that are created on the fly and are then ran on the Database.

The problem I have is that I can't fathom a way to display these results to the screen without first knowing what fields are used in the report. The queries could contain just 2 fields from a table, or 15 fields, and I'd like the code to be robust enough to handle this.

So far, this is the code I'm using:

        $.ajax({
                type: 'POST',
                url: urlLink,
                success: function (data) {
                        var Type = (data.recordType);
                        var Results = (data.results);
                        var Name = (data.name);
                        var Description = (data.description);
                        var Titles = (data.titles);

                        $('#reportName').text(Name);
                        $('#reportDescription').text(Description);

                        $('#listTable > tbody:last').empty();
                        $('#listTable > thead:last').empty();

                        $('#listTable > thead:last').append('<tr>'+Titles+'</tr>');

                        $.each(Results, function(i, item) {
                                $('#listTable > tbody:last').append('<tr><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td><td>' + Results[i] + '</td></tr>');
                        });
                }
        });

The variable Titles comes from the query, as when the user is adding fields to the database these are then added to a string which I then explode using PHP in the controller.

Inside the foreach, every column comes back with [object Object]. When I remove the [i] from the code and replace it with .column-name it will then work. But this is what I'm trying to avoid. I'd like to have something similar to what I do with the Table Titles.

  • 写回答

2条回答 默认 最新

  • douping5226 2014-11-24 10:52
    关注

    Maybe try this, and show output of console.log(data);

    console.log(data);
    var data = $.parseJSON(data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置