来自 AJAX 脚本的 JSON 数据未出现在 HTML 表中

发布于 2024-10-29 18:17:55 字数 2437 浏览 2 评论 0原文

这是我的代码:

$(document).ready(function () {

    $.ajax({
        url: '/blog/getposts',
        type: 'GET',
        dataType: 'json',
        success: function (result) {

            var insert = '';

            $.each(result, function (index, item) {
                insert += '<tr><td>' + item.Name + '</td><td>' + item.Body + '</td><td>' + item.DateCreated + '</td></tr>';
            });
            $('#blogTable').append(insert);
        }


    });
});

当我不尝试插入任何 标签时它会起作用,但当我包含这些标签时不会出现任何内容。我也有 Firebug,但我不知道要寻找什么。

我想显示一个表格,其中包含名称、正文和创建日期的标题,然后显示下面列出的每个条目的所有数据。

编辑:我的 JSON 结果

[
{"PostId":4,"UserId":2,"Body":"testtat","DateCreated":"\/Date(1301692095627)\/","Name":"derper"},  
{"PostId":3,"UserId":2,"Body":"tesateat","DateCreated":"\/Date(1301692093497)\/","Name":"derper"},  
{"PostId":2,"UserId":2,"Body":"testest","DateCreated":"\/Date(1301692091527)\/","Name":"derper"},  
{"PostId":1,"UserId":2,"Body":"derprperp","DateCreated":"\/Date(1301692082100)\/","Name":"derper"}
]

编辑:FireBug 的 InnerHTML 粘贴

<tbody><tr>
    <th>Name</th>
    <th>Body</th>
    <th>Date</th>
</tr>

编辑:所以这是我的 html:

<table id="blogTable">
<tr>
    <th>Name</th>
    <th>Body</th>
    <th>Date</th>
</tr>

如果我将插入更改为:

insert += '' + item.Name + '' + item.Body + '' + item.DateCreated + '';

那么我运行网站时的innerhtml输出是:

<tbody><tr>
    <th>Name</th>
    <th>Body</th>
    <th>Date</th>
</tr>
</tbody>
 <td>derper</td><td>testtat</td><td>/Date(1301692095627)/</td>
 <td>derper</td><td>tesateat</td><td>/Date(1301692093497)/</td>
 <td>derper</td><td>testest</td><td>/Date(1301692091527)/</td>
 <td>derper</td><td>derprperp</td><td>/Date(1301692082100)/</td>

所以我的问题是当我尝试添加新行时,它们不会显示,但是当我只使用单元格时( ),他们出现了。如何将新的 添加到 中?

Here is my code:

$(document).ready(function () {

    $.ajax({
        url: '/blog/getposts',
        type: 'GET',
        dataType: 'json',
        success: function (result) {

            var insert = '';

            $.each(result, function (index, item) {
                insert += '<tr><td>' + item.Name + '</td><td>' + item.Body + '</td><td>' + item.DateCreated + '</td></tr>';
            });
            $('#blogTable').append(insert);
        }


    });
});

It works when I don't try to insert any <tr> or <td> tags but nothing appears when I do include those tags. I also have Firebug but I don't know what to look for.

I want to display a table with headers for Name, Body, and DateCreated, then all the data for each entry listed below.

EDIT: my JSON results

[
{"PostId":4,"UserId":2,"Body":"testtat","DateCreated":"\/Date(1301692095627)\/","Name":"derper"},  
{"PostId":3,"UserId":2,"Body":"tesateat","DateCreated":"\/Date(1301692093497)\/","Name":"derper"},  
{"PostId":2,"UserId":2,"Body":"testest","DateCreated":"\/Date(1301692091527)\/","Name":"derper"},  
{"PostId":1,"UserId":2,"Body":"derprperp","DateCreated":"\/Date(1301692082100)\/","Name":"derper"}
]

EDIT: The InnerHTML paste from FireBug

<tbody><tr>
    <th>Name</th>
    <th>Body</th>
    <th>Date</th>
</tr>

EDIT: So this is my html:

<table id="blogTable">
<tr>
    <th>Name</th>
    <th>Body</th>
    <th>Date</th>
</tr>

If i change my insert to this:

insert += '<td>' + item.Name + '</td><td>' + item.Body + '</td><td>' + item.DateCreated + '</td>';

then the innerhtml output when i run the site is:

<tbody><tr>
    <th>Name</th>
    <th>Body</th>
    <th>Date</th>
</tr>
</tbody>
 <td>derper</td><td>testtat</td><td>/Date(1301692095627)/</td>
 <td>derper</td><td>tesateat</td><td>/Date(1301692093497)/</td>
 <td>derper</td><td>testest</td><td>/Date(1301692091527)/</td>
 <td>derper</td><td>derprperp</td><td>/Date(1301692082100)/</td>

so my problem is when I'm trying to add new rows, they don't show up, but when I just use cells (<td>), they show up. How can I add new <tr>'s into the <tbody>?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情废物 2024-11-05 18:17:55

到目前为止,您发布的代码是有效的。  请参阅: jsfiddle.net/SQVz6/

使用 Firebug 检查表:blogTable。 (右键单击表格通常会在上下文菜单中显示 Firebug 的“检查”工具。)

使用 Firebug 检查表格源。

~~~~~~~
表结构看起来正确吗?
在检查器中右键单击,然后单击复制innerHTML
将结果粘贴到上面的问题中。

The code you've posted, so far, works.   See: jsfiddle.net/SQVz6/ .

Using Firebug, inspect table: blogTable. (Right-clicking on the table will usually show Firebug's "Inspect" tool in the context menu.)

Inspecting the table source using Firebug.

~~~~~~~
Does the table structure look right?
Right-click on <table id="blogTable"> in the inspector and then click Copy innerHTML
Paste the results into your question, above.

眼眸里的快感 2024-11-05 18:17:55

尝试将 .append() 更改为 .appendTo() 吗?

Try changing .append() to .appendTo()?

一个人的旅程 2024-11-05 18:17:55

经过几个小时的尝试找出解决方案后,我将插入语句更改为:

 $('#blogTable tr:last').after(insert);

现在它可以工作了。 innerHTML 输出如下:

<tbody>
<tr>
    <th>Name</th>
    <th>Body</th>
    <th>Date</th>
</tr>
<tr>
    <td>derper</td>
    <td>testtat</td>
    <td>/Date(1301692095627)/</td>
</tr>
<tr>
    <td>derper</td>
    <td>tesateat</td>
    <td>/Date(1301692093497)/</td>
</tr>
<tr>
    <td>derper</td>
    <td>testest</td>
    <td>/Date(1301692091527)/</td>
</tr>
<tr>
    <td>derper</td>
    <td>derprperp</td>
    <td>/Date(1301692082100)/</td>
</tr>
</tbody>

after many hours of trying to figure out a solution, i changed the insert statement to this:

 $('#blogTable tr:last').after(insert);

and it works now. innerHTML output came out as follows:

<tbody>
<tr>
    <th>Name</th>
    <th>Body</th>
    <th>Date</th>
</tr>
<tr>
    <td>derper</td>
    <td>testtat</td>
    <td>/Date(1301692095627)/</td>
</tr>
<tr>
    <td>derper</td>
    <td>tesateat</td>
    <td>/Date(1301692093497)/</td>
</tr>
<tr>
    <td>derper</td>
    <td>testest</td>
    <td>/Date(1301692091527)/</td>
</tr>
<tr>
    <td>derper</td>
    <td>derprperp</td>
    <td>/Date(1301692082100)/</td>
</tr>
</tbody>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文