[英]jquery ajax post with json content type returning bad request as HTML
I have the below example code which works in my local development environment but when publishing to a live environment which uses HTTPS and uses the .NET bundling for the javascript, and ELMAH for error logging, this no longer works as intended. 我有以下示例代码可在我的本地开发环境中使用,但是当发布到使用HTTPS并使用.NET绑定的javascript和ELMAH进行错误记录的实时环境中时,此代码将无法正常工作。
Instead of a JSON content response I get a HTML content response with the responseText
"Bad Request" and no responseJSON
property so this code results in a javascript error. 我得到的不是HTML内容响应,而是带有responseText
“ Bad Request”和没有responseJSON
属性的HTML内容响应,因此此代码导致JavaScript错误。
Does anyone know why the content type would get changed? 有谁知道为什么内容类型会改变? presumably due to this being in a live environment and a response code of 400? 大概是因为这是在实时环境中,响应码为400? but I'm not sure what is going on here. 但我不确定这是怎么回事。
Controller: 控制器:
public JsonResult JsonModelErrorResult()
{
Response.StatusCode = 400;
var errors = ModelState.Values.SelectMany(m => m.Errors);
return Json(errors);
}
[HttpPost]
public ActionResult GetData()
{
...
if (results != null && results.Any())
{
return Json(result, JsonRequestBehavior.AllowGet);
}
else
{
ModelState.AddModelError("SearchResults", "No results found");
return this.JsonModelErrorResult();
}
}
Javascript: 使用Javascript:
$.ajax("/Controller/GetData/", {
dataType: "json",
type: "POST",
contentType: "application/json"
})
.done((result) => {
})
.fail((xhr) => {
setTimeout(() => {
this.errors(xhr.responseJSON);
}, 200);
})
.always(() => {
});
Update: 更新:
This is the response header when I view the response for the request in Chrome: 当我在Chrome中查看请求的响应时,这是响应头:
HTTP/1.1 400 Bad Request
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: text/html
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
Persistent-Auth: true
X-Powered-By: ASP.NET
Date: Thu, 03 Jul 2014 12:08:23 GMT
Content-Length: 11
and the returned value from the ajax call is a Jquery jqXHR object with a responseText
property of "Bad Request"
and a content type of "text/html"
并且从ajax调用返回的值是一个Jquery jqXHR对象,其responseText
属性为"Bad Request"
,内容类型为"text/html"
UPDATE 2: This is the custom errors setup in my web.config 更新2:这是我的web.config中的自定义错误设置
<customErrors mode="RemoteOnly" defaultRedirect="~/Error">
<error statusCode="401" redirect="~/Error/NotAuthorised" />
<error statusCode="403" redirect="~/Error/NotAuthorised" />
<error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>
When testing I changed the mode to "Off" but this did not work in the live environment which is IIS8, maybe I missed something that need to be updated in order for IIS to do this correctly or that the defaultRedirect="~/Error">
should have also been removed? 在测试时,我将模式更改为“关闭”,但是在IIS8的实时环境中不起作用,也许我错过了一些需要更新的内容,以便IIS正确执行此操作或defaultRedirect="~/Error">
是否也应该删除? but adding the line Response.TrySkipIisCustomErrors = true;
但是添加行Response.TrySkipIisCustomErrors = true;
into the JsonModelErrorResult code has stopped this content/html error with the line "Bad Request" being returned. 进入JsonModelErrorResult代码已停止了此content / html错误,并返回了“错误请求”行。
Right after you set the Response.StatusCode
add this line of code: 在设置Response.StatusCode
添加以下代码行:
Response.TrySkipIisCustomErrors = true;
This tells IIS not to intercept the request and use its own error page. 这告诉IIS不要拦截请求并使用其自己的错误页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.