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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?