【发布时间】:2017-01-18 04:00:07
【问题描述】:
我想在页面加载时使用 ajax 从数据库中加载一些数据。但它不会工作或显示数据。
我在控制器端的代码。
public ActionResult Index(int? respondentId)
{
return View();
}
[HttpPost]
public ActionResult GetIntroParagraph(int? id)
{
int id = id?? 111;
Paragraph p= ParagraphDb.GetParagraghByRespondentId(id);
return Content(p.Introduction);
}
还有我在视图上的代码,
<script>
$(function () {
var data = undefined;
$.load(@Url.Action("GetIntroParagraph","Landing"),data,function(result) {
$('#paragraph').append(result);
});
});
</script>
<div id="paragraph"></div>
我哪里弄错了?
【问题讨论】:
-
如果您在浏览器控制台中遇到任何错误怎么办?
p.Introduction有值吗? -
@Stephen Muecke 'p.Introduction' 没有价值。我设置了一个断点。它不会进入 GetIntroParagraph 方法。
-
那么您在浏览器中遇到了什么错误? (以及您在
'@Url.Action("GetIntroParagraph","Landing")'周围缺少的引号) -
@Stephen Muecke 感谢您在控制台中检查错误的提示,现在我可以正常工作了。我应该使用 $.get 或 $.post,它不会识别 $.load。
-
.load()的语法错误 - 它必须是$(someElement).load(url, data);- 请参阅 documentation
标签: jquery .net ajax asp.net-mvc