使用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.

douwuli4512
douwuli4512 我没有,你能详细说明吗?
接近 6 年之前 回复
dpbl91234
dpbl91234 你知道(varxina)javascript语法吗?
接近 6 年之前 回复

2个回答



也许试试这个,并显示console.log(数据)的输出; </ p>

  console.log(data); 
var data = $ .parseJSON(data);
</ code> </ pre>
</ div>

展开原文

原文

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

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

douyan8413
douyan8413 由于声誉低,我无法发表评论
接近 6 年之前 回复
dongsi4815
dongsi4815 这应该是评论,而不是答案。
接近 6 年之前 回复



  $。each(data,function(){
$ .each(this,function(k,v){

///做东西
});
});
</ code> </ pre>
</ div>

展开原文

原文

$.each(data, function() {
  $.each(this, function(k, v) {
 /// do stuff
});
});

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问