在出现异常的情况下,在用户计算机上获取 text/html 响应而不是 application/json
Getting text/html Response instead of application/json on user machine in case of Exception
我遇到 JSON 响应的奇怪问题。
MVC 应用程序 A 托管在服务器 A 上。当用户浏览 URL 时,它在内部调用 MVC 操作以使用 JQuery Ajax 在 UI 中获取数据。
MVC 动作:
Try
Return Json(mRetResponse, JsonRequestBehavior.AllowGet)
Catch ex As Exception
HttpContext.Response.StatusCode = System.Net.HttpStatusCode.InternalServerError
Dim mErrorMessage As String = ex.Message.Replace("Error -", "")
<<ObjErrorResponse is created here by assigning error message.>>
Return Json(ObjErrorResponse, JsonRequestBehavior.AllowGet)
End Try
JQUERY 调用
CommonJS.ShowProgress();
CommonJS.ajaxPost(_Controller + "/Delete"
, 'json'
, { // dataParam // }
, function (msg) {
CommonJS.HideProgress();
var responseObj = msg;
if (responseObj.Success) {
if ((responseObj.Data) && typeof responseObj.Data === 'string') {
alert(responseObj.Data);
}
//Processing goes here
}
else {
CommonJS.ShowErrors(responseObj);
}
}
, function (jqxhr, textStatus, error) {
CommonJS.HandleErrors(jqxhr);
}
, true
);
当我们在用户机器(而不是服务器机器)上浏览 MVC 应用程序 URL 时,我们得到以下行为
- 万一成功;我们在服务器机器和用户机器上都得到了正确的JSON。
- 如果出现异常,我们会发送 JSON 最终显示的错误响应
给用户的一些错误信息。 - 当我们在服务器计算机上实际浏览 MVC URL 时,它工作正常。然而,我们只在用户机器上遇到问题,它在用户浏览器中显示 text/html 作为响应。而不是 application/json.
来自用户机器的更多信息。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>500 - Internal server error.</h2>
<h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
</fieldset></div>
</div>
</body>
</html>
预期响应:
{"Success":false,"Data":null,"Errors":[{"ErrorMessage":"Unable to peform Delete operation."}],"CustomData":{}}
除了设置状态代码外,您还需要在响应中设置 the TrySkipCustomIisErrors
property。否则,IIS 将看到 500
响应和 return 其自定义“内部服务器错误”页面,而不是您自己的 JSON 响应。
我遇到 JSON 响应的奇怪问题。
MVC 应用程序 A 托管在服务器 A 上。当用户浏览 URL 时,它在内部调用 MVC 操作以使用 JQuery Ajax 在 UI 中获取数据。
MVC 动作:
Try
Return Json(mRetResponse, JsonRequestBehavior.AllowGet)
Catch ex As Exception
HttpContext.Response.StatusCode = System.Net.HttpStatusCode.InternalServerError
Dim mErrorMessage As String = ex.Message.Replace("Error -", "")
<<ObjErrorResponse is created here by assigning error message.>>
Return Json(ObjErrorResponse, JsonRequestBehavior.AllowGet)
End Try
JQUERY 调用
CommonJS.ShowProgress();
CommonJS.ajaxPost(_Controller + "/Delete"
, 'json'
, { // dataParam // }
, function (msg) {
CommonJS.HideProgress();
var responseObj = msg;
if (responseObj.Success) {
if ((responseObj.Data) && typeof responseObj.Data === 'string') {
alert(responseObj.Data);
}
//Processing goes here
}
else {
CommonJS.ShowErrors(responseObj);
}
}
, function (jqxhr, textStatus, error) {
CommonJS.HandleErrors(jqxhr);
}
, true
);
当我们在用户机器(而不是服务器机器)上浏览 MVC 应用程序 URL 时,我们得到以下行为
- 万一成功;我们在服务器机器和用户机器上都得到了正确的JSON。
- 如果出现异常,我们会发送 JSON 最终显示的错误响应 给用户的一些错误信息。 - 当我们在服务器计算机上实际浏览 MVC URL 时,它工作正常。然而,我们只在用户机器上遇到问题,它在用户浏览器中显示 text/html 作为响应。而不是 application/json.
来自用户机器的更多信息。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>500 - Internal server error.</h2>
<h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
</fieldset></div>
</div>
</body>
</html>
预期响应:
{"Success":false,"Data":null,"Errors":[{"ErrorMessage":"Unable to peform Delete operation."}],"CustomData":{}}
除了设置状态代码外,您还需要在响应中设置 the TrySkipCustomIisErrors
property。否则,IIS 将看到 500
响应和 return 其自定义“内部服务器错误”页面,而不是您自己的 JSON 响应。