【问题标题】:ASP.NET MVC return JSON() does show me a Save file DialogASP.NET MVC return JSON() 确实显示了一个保存文件对话框
【发布时间】:2009-05-22 08:11:49
【问题描述】:

我是 .NET MVC 模式的新手,并尝试在我的控制器中使用 AJAX.BeginForm Helper 类调用 Action 我使用 return JSON() 返回序列化 JSON;

在我看来,我添加了一个脚本女巫应该使用返回的 JSON。

   function ResultSet(request) {

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

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

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

为什么会这样?

【问题讨论】:

    标签: jquery asp.net-mvc json


    【解决方案1】:

    我遇到了类似的问题,请尝试确保您添加了对 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>
    

    【讨论】:

      【解决方案2】:

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

      Response.ContentType = "application/json";
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多