使用jQuery和Ajax使用json数据填充表

时间:2022-12-04 18:45:33

So I'm trying to populate a table with data from a script. I've tried lots changing lots of parameters etc but can't seem to get it to work. Here's what I've got so far:

所以我试图用脚本中的数据填充表格。我已经尝试过很多改变很多参数等但似乎无法让它工作。这是我到目前为止所得到的:

$(document).ready(function () {
        $.get(file + '?op=retrieve&page=1', function (data) {
            $.each(data, function (i, elem) {
                $('#mainTable').append(
                    '<tr><td>' + elem.name + '</td><td>' + elem.address  '</td></tr>'
                )
            })
        })

I'm currently getting the error: TypeError: e is not an Object. (evaluating 't-1 in e')

我目前收到错误:TypeError:e不是Object。 (评估't'中的't')

Heres a example of the json data:

下面是json数据的一个例子:

{"id":"348141","name":"Birdies","address":"Bridge Street, Tunbridge Wells, TN1 1AH"}]

1 个解决方案

#1


0  

From your data, I understood that you are getting an array of js objects.

从您的数据中,我了解到您正在获取一组js对象。

If you see your js code, you are using the following snippet

如果您看到了js代码,则表示您正在使用以下代码段

$.each(data, function (i, elem) { });

$ .each(data,function(i,elem){});

So from the above code, "i" is the index and elem is the current iterating value.

所以从上面的代码中,“i”是索引,elem是当前的迭代值。

So elem.name[i] would be invalid. This is because elem itself is an object which has name property and name is just of type string. So you cannot have name[i].

因此elem.name [i]将无效。这是因为elem本身是一个具有name属性的对象,name只是string类型。所以你不能有名字[i]。

$(document).ready(function () {
        $.get(file + '?op=retrieve&page=1', function (data) {
            $.each(data, function (i, elem) {
                $('#mainTable').append(
                    '<tr><td>' + elem.name + '</td><td>' + elem.address  '</td></tr>'
                )
            })
        })

I assume that "mainTable" element looks something like this

我假设“mainTable”元素看起来像这样

<table>
<tbody id="mainTable">
</tbody>
</table>

#1


0  

From your data, I understood that you are getting an array of js objects.

从您的数据中,我了解到您正在获取一组js对象。

If you see your js code, you are using the following snippet

如果您看到了js代码,则表示您正在使用以下代码段

$.each(data, function (i, elem) { });

$ .each(data,function(i,elem){});

So from the above code, "i" is the index and elem is the current iterating value.

所以从上面的代码中,“i”是索引,elem是当前的迭代值。

So elem.name[i] would be invalid. This is because elem itself is an object which has name property and name is just of type string. So you cannot have name[i].

因此elem.name [i]将无效。这是因为elem本身是一个具有name属性的对象,name只是string类型。所以你不能有名字[i]。

$(document).ready(function () {
        $.get(file + '?op=retrieve&page=1', function (data) {
            $.each(data, function (i, elem) {
                $('#mainTable').append(
                    '<tr><td>' + elem.name + '</td><td>' + elem.address  '</td></tr>'
                )
            })
        })

I assume that "mainTable" element looks something like this

我假设“mainTable”元素看起来像这样

<table>
<tbody id="mainTable">
</tbody>
</table>