已部署软件上第 1 行错误消息中的 JSON 无效字符

本文关键字:消息 JSON 字符 无效 错误 软件 部署 | 更新日期: 2023-09-27 18:37:17

我在已部署的软件上收到上述错误消息,我的问题是在运行本地主机时没有出现错误。

我已经检查了本地主机和服务器上的 json 中是否输入了有效数据,但我仍然收到此错误,因为 json.parse 正在尝试解析 html。我不知所措,任何人都可以帮助我弄清楚我的控制器和我的 javascript 之间的 json 发生了什么?

控制器:

public ActionResult UserStatuses_Read([DataSourceRequest]DataSourceRequest request, string userName)
{
    try
    {
        throw new DivideByZeroException();
        return Json("Hello World");
    }
    catch (Exception e)
    {
        ExceptionContext filterContext = new ExceptionContext();
        HttpContext.Response.StatusCode = 500;
        return Json(new
        {
            errorCode = (HttpContext.Response.StatusCode),
            Title = "Error",
            Details = e.Message
        });
    }
}

Javascript:

function onError(args, status) {
    var kendoNotification = $("#Notification").data("kendoNotification");
    var errorMessage = JSON.parse(args.xhr.responseText);
    kendoNotification.show({
        title: errorMessage.Title,
        message: errorMessage.Details
    }, "error");
};

额外信息:上面的代码应该在抛出错误时显示通知。正如我上面提到的,这在本地主机运行时效果很好,但是当通过我们的服务器运行时,正在解析的数据是如下所示的 html 代码。为什么我的错误通知会导致错误?任何帮助将不胜感激。提前谢谢。

来自 JSON 的 HTML:

<!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>

已部署软件上第 1 行错误消息中的 JSON 无效字符

当你抛出一个 500 错误时,IIS 会包装在 html 中返回的所有内容,因此你的 json 将在 html 中丢失。

尝试在设置错误代码的位置添加下面的代码行。

Response.TrySkipIisCustomErrors = true;

这应该会强制您的 json 通过,并允许您在自己的自定义错误处理程序中使用它。

根据您的回答,我将说本地主机上的路径与远程服务器上的路径不同。 在本地 Web 服务器上运行代码时,路径通常类似于 http://localhost:port/page/parameters。 但是,当代码在远程服务器上运行时,路径类似于 http://servername/applicationname/page/parameters。

检查以确保视图和 javascript 中的任何链接(图像、页面等)都可以在它们前面附加应用程序名称,以便它们在本地主机和远程服务器上工作。