.net mvc4中使用jquery的getJSON()获取不到json

Controller:

         public JsonResult Cars(int id)
        {
            JsonResult res = new JsonResult();
            Cars entity = car.Cars.SingleOrDefault(a => a.CarNo == id);
            res.Data = entity;
            return Json(res, JsonRequestBehavior.AllowGet); 
        }

View:

 <script type="text/javascript">
        $("button").click(function () {
            $.getJSON("/Compare/Cars?id=7", null,function (res) {
                    $("#name").html(res.CarNo);
                });
            });
</script>
<button>获得 JSON 数据</button>
<div id= "name"></div>

不知道为什么,获取不到json,求大神解答一下

从给出的代码看应该是按钮单击事件就没绑定上,将按钮单击事件写在$(function(){})里面试下, $(function(){ $("button").click(function () {
$.getJSON("/Compare/Cars?id=7", null,function (res) {
$("#name").html(res.CarNo);
});
});
})