圆滚滚的胖狸 2017-08-23 06:25 采纳率: 80%
浏览 3461
已采纳

求助ajax执行后,会自动刷新页面,该如何阻止?

asp.net mvc
前台代码:

 <html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>TestUpload</title>
    <script type="text/javascript" src="~/Scripts/jquery-1.10.2.min.js"></script>
</head>
<body>
    <div>
        <textarea id="txt"></textarea>
        <form id="uploadForm" action="Upload/TestAjax" method="post" enctype="multipart/form-data">
            <input id="File1" name="fileupload" accept="image/gif, image/jpeg" multiple="multiple" type="file" value="" />
            <input id="btn" type="button" value="上传" />
        </form>

    </div>
</body>
</html>
<script type="text/javascript">
    $(function () {
        $("#btn").click(function () {
            var formData = new FormData($("#uploadForm")[0]);
            //var formData = $("#uploadForm");
            $.ajax({
                url: "/Upload/TestAjax",
                data: formData,
                type: "POST",
                cache: false,
                async: true,
                contentType: false,
                processData: false,
                success: function (data) {
                    console.log(data);

                },
                error: function (data) {
                    console.log(data);
                },
            });            
            return false;

        });
    });
</script>

后台代码

  public ActionResult TestUpload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult TestAjax()
        {

            if (Request.Files.Count > 0)
            {
                if (Request.Files.Count == 1)
                {
                    HttpPostedFileBase file = Request.Files[0];
                    if (file.ContentLength > 0)
                    {
                        string title = string.Empty;
                        title = DateTime.Now.Ticks.ToString() + Path.GetExtension(file.FileName);
                        string path = "/upload/" + DateTime.Now.ToString("yyyyMMdd")+"/" + title;
                        path = System.Web.HttpContext.Current.Server.MapPath(path);
                        file.SaveAs(path);
                        return Json(new { status = true, url = path });
                    }
                }
                else
                {
                    string[] urllist = new string[Request.Files.Count];
                    for (int i = 0; i < Request.Files.Count; i++)
                    {
                        HttpPostedFileBase file = Request.Files[i];
                        if (file.ContentLength > 0)
                        {
                            string title = string.Empty;
                            title = DateTime.Now.Ticks.ToString() +Path.GetExtension(file.FileName);
                            string path = "/upload/" + DateTime.Now.ToString("yyyyMMdd") + "/" + title;
                            path = System.Web.HttpContext.Current.Server.MapPath(path);
                            file.SaveAs(path);
                            urllist[i] = path;
                        }
                    }
                    return Json(new { status = true, url = urllist });
                }

            }
            else
            {
                return Json(new { status = false, url = "", msg = "没有文件" });
            }



            return Json(new { status = false, url = "", msg = "" });



        }


其实就是一个简单的ajax文件上传。我的要求是,执行成功不要刷新页面。
但是很奇怪的是,执行不成功时,代码不刷新,一旦执行成功就自动给我刷新了!!!跪求各位大大
开发环境WIN10+VS2015

  • 写回答

9条回答 默认 最新

  • 圆滚滚的胖狸 2017-08-23 07:22
    关注

    我找到了解决方法,感谢各位。
    解决方法在web.config中加入

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 关于c++外部库文件宏的问题,求解
  • ¥15 office打开卡退(新电脑重装office系统后)
  • ¥300 FLUENT 火箭发动机燃烧EDC仿真
  • ¥15 【Hadoop 问题】Hadoop编译所遇问题hadoop-common: make failed with error code 2
  • ¥15 vb6.0+webbrowser无法加载某个网页求解
  • ¥15 RPA财务机器人采购付款流程
  • ¥15 计算机图形多边形及三次样条曲线绘制
  • ¥15 根据protues画的图用keil写程序
  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误