【发布时间】:2010-06-22 17:37:41
【问题描述】:
我正在使用this blog post as a guide,详细说明如何使用带有 jTemplates 的 jQuery 将 JSON 响应填充到模板中。
我的问题是,返回的字段之一(名为描述)包含 HTML,但 HTML 括号被编码为 \u003C 和 \u003e。
这是服务器返回的 HTML(在描述字段中):
<a href="/en/Yokota/User/Show/Chad">Chad</a> updated their ad, <a href="/en/Yokota/Ad/Show/100">Validation Test Ad Again</a>, @<a href="/en/Yokota">Yokota</a>
JSON 响应如下所示:
[{"TypeOfActivity":"0","Description":"\u003ca href=\"/en/Yokota/User/Show/YokotaTour\"\u003eYokotaTour\u003c/a\u003e posted \u003ca href=\"/en/Yokota/Ad/Show/166\"\u003eOne of the best for sure\u003c/a\u003e for sale @\u003ca href=\"/en/Yokota\"\u003eYokota\u003c/a\u003e","DateHappened":"6/23/2010 12:26:55 AM"}]
注意“\u003c”或“\u003e”。那些看起来像 unicode 转义,但为什么会这样呢?这是调用 JSON 响应的 jQuery:
$.getJSON("<%= Url.Action("List", "Activity") %>",
function(data){
$("#aLog").setTemplate($("#templateHolder").html());
$("#aLog").processTemplate(data);
});
更新
这是页面加载完成后源代码的样子(在 Firefox 中查看 > 页面源代码):
<a href="/en/Yokota/User/Show/Chad">Chad</a> updated their ad, <a href="/en/Yokota/Ad/Show/100">Validation Test Ad Again</a>, @<a href="/en/Yokota">Yokota</a>
也许是因为快到凌晨 3 点了,但我很难过……非常感谢任何帮助 - 谢谢!
更新 2
public JsonResult List()
{
IList<ActivityContract> contracts = new List<ActivityContract>();
var activityList = _db.Activity.ByBaseID(CurrentBase.BaseID).OrderByDescending(a => a.DateHappened);
foreach (var a in activityList) {
contracts.Add(new ActivityContract { TypeOfActivity = a.TypeOfActivity.ToString(), Description = a.Description, DateHappened = a.DateHappened.ToString() });
}
return Json(contracts, JsonRequestBehavior.AllowGet);
}
【问题讨论】:
-
您的操作方法如何返回值?作为 JsonResult?我们可以看到代码吗?
-
processTemplate是做什么的? -
@womp 我添加了控制器代码。感谢您的快速回复。
-
@Gumbo - 不确定,这是微软对 jQuery 团队的贡献,jtemplates.tpython.com
-
jTemplates != MS jQuery 模板库。这是完全不同的。在我看来,模板引擎正在做你告诉它做的事情。您正在给它显示数据,它正在显示它。如果您打算将数据设为 HTML,则引擎需要知道这一点。我不知道 jTemplates,所以我不能说它是如何或是否是一个特性。
标签: c# jquery json asp.net-mvc-2 jtemplate