来自 AJAX 脚本的 JSON 数据未出现在 HTML 表中
这是我的代码:
$(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
到目前为止,您发布的代码是有效的。 请参阅: jsfiddle.net/SQVz6/ 。
使用 Firebug 检查表:
blogTable
。 (右键单击表格通常会在上下文菜单中显示 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.)~~~~~~~
Does the table structure look right?
Right-click on
<table id="blogTable">
in the inspector and then clickCopy innerHTML
Paste the results into your question, above.
尝试将
.append()
更改为.appendTo()
吗?Try changing
.append()
to.appendTo()
?经过几个小时的尝试找出解决方案后,我将插入语句更改为:
现在它可以工作了。 innerHTML 输出如下:
after many hours of trying to figure out a solution, i changed the insert statement to this:
and it works now. innerHTML output came out as follows: