ASP.NET MVC返回JSON()确实向我显示了一个Save file对话框

时间:2009-05-22 08:11:49

标签: jquery asp.net-mvc json

我是.NET MVC模式的新手,并尝试在我的Controller中使用AJAX.BeginForm Helper类调用Action我使用return JSON()返回seralized JSON;

在我看来,我添加了一个Scrip女巫应该消耗返回JSON。

   function ResultSet(request) {

   var json = content.get_response().get_object();
   var result = eval(json);

   if (result.Successfull) {
       alert("Success!");
   }
   else {
       alert("else");
       }
   }

但是,如果我返回,浏览器会显示一个保存对话框以保存JSON文件。

为什么?

3 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,请确保您添加了对microsoft ajax库的引用:

<script src="/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>

也。你的ResultSet()方法将一个名为“request”的变量作为参数,我认为它应该被称为“content”,就像这样?

<script type="text/javascript">
    function ResultSet(content) {
        var json = content.get_response().get_object();

        var result = eval(json);
        if (result.Successfull) {
            alert("Success!");
        }
        else {
            alert("else");
        }
    }    
</script>

答案 1 :(得分:0)

尝试设置 application / json 响应的内容类型,如下所示:

Response.ContentType = "application/json";

答案 2 :(得分:0)

你忘了引用jQuery了。把它放在你的html的头部:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>