我遇到了 JSON 回應的奇怪問題。
MVC 應用程式 A 托管在服務器 A 上。當用戶瀏覽內部呼叫 MVC 操作以使用 JQuery Ajax 在 UI 中獲取資料的 URL 時。
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 作為回應。而不是應用程式/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":{}}
uj5u.com熱心網友回復:
除了設定狀態代碼之外,您還需要設定回應的TrySkipCustomIisErrors屬性。否則,IIS 將看到500回應并回傳其自定義的“內部服務器錯誤”頁面,而不是您自己的 JSON 回應。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/381066.html
標籤:C# 查询 json asp.net-mvc 信息系统
上一篇:使用urlAction為標簽賦值
下一篇:帶角色的用戶注冊
