Javascript MVC控制器JsonResult错误服务器无法在发送HTTP头后设置内容类型,而返回
在控制器中具有此功能:Javascript MVC控制器JsonResult错误服务器无法在发送HTTP头后设置内容类型,而返回,javascript,jquery,json,asp.net-mvc,Javascript,Jquery,Json,Asp.net Mvc,在控制器中具有此功能: public async Task<JsonResult> getTipoPerfil(int id) { PerfilesCalificacion pc = await db.PerfilesCalificaciones.FindAsync(id); int ret = -1; if (pc!=null) { switch (pc.Tipo)
public async Task<JsonResult> getTipoPerfil(int id)
{
PerfilesCalificacion pc = await db.PerfilesCalificaciones.FindAsync(id);
int ret = -1;
if (pc!=null)
{
switch (pc.Tipo)
{
case TipoCalificacion.NotaGlobal:
ret = 0;
break;
case TipoCalificacion.PerfilCalificacion:
ret = 1;
break;
default:
ret = -1; ;
break;
}
}
return new JsonResult { Data = ret };
}
非常感谢 孤立的代码似乎很好。您是否有其他正在设置一些响应头的底层代码?您是否有其他端点成功返回JSON?您好,我从global.asax添加了一些代码。所有其他JSON都在安全返回。我用其他类型的解决方案解决了这个问题。。。但是我想知道为什么会发生这样的事情Fancisco Mc Manus,地狱里有一个特别的地方,留给那些说他们找到了解决StackOverflow问题的方法,然后不分享解决方案的人:)噢,Michael,我没有解决这个问题。。。也许我的英语不是很好。我想说的是,我没有使用这个代码。隔离的代码看起来很好。您是否有其他正在设置一些响应头的底层代码?您是否有其他端点成功返回JSON?您好,我从global.asax添加了一些代码。所有其他JSON都在安全返回。我用其他类型的解决方案解决了这个问题。。。但是我想知道为什么会发生这样的事情Fancisco Mc Manus,地狱里有一个特别的地方,留给那些说他们找到了解决StackOverflow问题的方法,然后不分享解决方案的人:)噢,Michael,我没有解决这个问题。。。也许我的英语不是很好。我想说的是我没有使用这个代码。
.
.
.
@Html.DropDownListFor(model => model.ApiTdEx.PerfilCalificacionId, (List<SelectListItem>)ViewBag.LstPerfiles, new { @class = "form-control", @onchange = "MuestraPerfil()" })
.
.
.
<script>
function MuestraPerfil() {
var idperfil = document.getElementById("ApiTdEx_PerfilCalificacionId").value;
$.ajax({
type: 'POST',
url: '@Url.Action("getTipoPerfil", "MenuProfesor")',
dataType: 'json',
data: { id: $("#ApiTdEx_PerfilCalificacionId").val() },
success: function (tipo) {
alert(tipo);
},
error: function (request, status, error) {
alert(error);
}
});
}
</script>
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.BufferOutput = true;
string culture = null;
if (context.Request.UserLanguages != null && Request.UserLanguages.Length > 0)
{
culture = Request.UserLanguages[0];
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
}