$ .getJSON()在ASP.NET MVC4中不起作用

时间:2013-02-21 11:23:01

标签: asp.net-mvc jquery

这真的很奇怪,我不知道我哪里出错了。

这是我的控制器:

    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根本不起作用&amp;我甚至没有得到警报。怎么了?

3 个答案:

答案 0 :(得分:1)

从视图中尝试Ajax帖子,比如

 $.ajax({
 url: '@Url.Action(action)',
  data: data
  type: 'POST',
  success: function (result) {

                              }
                          });

答案 1 :(得分:1)

你在哪里包含jquery文件? Jquery文件必须首先编写自己的javascript代码..

并尝试此代码;

$(document).ready(function () {
    $('#button1').click(function () {
        $.getJSON("/Home/Called", function (retObject) {
            alert('Working!');
            $('#p1').text("Roll Number was " + retObject.rollNo + " and the Name was " + retObject.name);
        });
    });
});

尝试将return Json(retObject);替换为return Json(retObject,JsonRequestBehavior.AllowGet);

答案 2 :(得分:0)

***指只在你的Mvc控制器功能中写入这一行

  

Configuration.ProxyCreationEnabled = false;