首页 新闻 会员 周边

为什么当JsonResult 返回contentType:text/html的时候会报302错误?

0
悬赏园豆:10 [待解决问题]
    public JsonResult UpLoadFile()
        {
//其他代码
...

                          return this.Json("{msg:sucess}" ,"text/html;charset=utf-8", JsonRequestBehavior.AllowGet);
}

这是一个上传文件的代码,
之前这段代码一直没有问题,
最近发现批量上传几百个文件会报302的错误,也就是没有登录。
刚开始一部分文件上传是没问题的,我跟踪代码发现,后面这个session找不到过期了,cookie也获取不到。
把代码修改为:

 return this.Json("{msg:sucess}" , JsonRequestBehavior.AllowGet);

就不报错了,
虽然错误修复了,但是我很好奇,为什么会这样?

极无宪的主页 极无宪 | 初学一级 | 园豆:37
提问于:2020-09-15 05:24
< >
分享
所有回答(2)
0

这个返回的是json格式,你前端使用的ajax解析的吗?

$.ajax({
url: "You Url",
type: "get",
data: {
"curPage": pagenow
...
},
dataType: "json",
beforeSend: function (jqXHR) {
},

你看看解析的是

text/html的意思是将文件的content-type设置为text/html的形式,浏览器在获取到这种文件时会自动调用html的解析器对文件进行相应的处理。

如何用.NET mvc和webapi实现文件上传:https://www.52interview.com/solutions/27

智客工坊 | 园豆:1855 (小虾三级) | 2020-09-15 10:52
0
 System.Web.HttpException (0x80004005): 服务器无法在发送 HTTP 标头之后设置内容类型。
   在 System.Web.HttpResponse.set_ContentType(String value)
   在 System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context)
   在 System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
   在 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
   在 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
   在 System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult)
   在 System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   在 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   在 System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   在 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
   在 System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
   在 System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   在 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   在 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
   在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

后台报错,HttpException 报错之后session,cookie都混乱了,所以返回302没有权限操作

极无宪 | 园豆:37 (初学一级) | 2020-09-15 15:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册