这真的很奇怪,我不知道自己出错的地方。
这里是我的控制器:
namespace MvcAJAX.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Called()
{
var retObject = new { rollNo = 2, name = "Deepanjan" };
return Json(retObject);
}
}
}
这里是我的索引视图:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script>
/// <reference path="~/Scripts/jquery-1.7.1.js" />
$(document).ready(function() {
$('#button1').click(function() {
$.getJSON("/Home/Called",function() {
alert('Working!');
$('#p1').text("Roll Number was " + data.rollNo + " and the Name was " + data.name);
});
});
});
</script>
<h1>This page demos Load with callback function!</h1>
<input type="button" id="button1" value="Click Me!"/><br />
<p style="height:200px" id="p1"></p>
<hr />
我的getJSON根本不起作用&我甚至不得到警报。怎么了?
检查您的浏览器控制台是否有任何错误。你有没有检查点击事件是否被调用? – 2013-02-21 11:26:37
试着用'return Json(retObject,JsonRequestBehavior.AllowGet)替换'return Json(retObject);''' – 2013-02-21 11:41:06