gpt4 book ai didi

c# - 为什么我的 ajax 帖子被截断了?

转载 作者:可可西里 更新时间:2023-11-01 07:59:00 26 4
gpt4 key购买 nike

我刚刚更新了我的 mvc 服务以包含更多错误和日志记录。我现在已经多次遇到这个确切的错误。但不能复制。

Unterminated string. Expected delimiter: ". Path 'Breadcrumbs[18].Params', line 1, position 59740. at Newtonsoft.Json.JsonTextReader.ReadStringIntoBuffer(Char quote) at 

路径每次都不同,具体取决于用户向服务器发送的内容。

我的 ajax 请求通常是这样的:

$.ajax(myURL("SendBreadcrumbs"), {
type: "POST",
cache: false,
data: { telemetry: telemetry.JSONify(), userID: currentUser, isMyVI: isMyVI }
})

在这些情况下,userID 和 isMyVI( bool 值)不存在,遥测字符串被截断。

JSONify方法如下:

self.JSONify = function () {
var data = ko.mapping.toJSON(self, TelemetryMapping);
return data;
}

这是 knockoutJSs 序列化器。

服务器端:

public void SendBreadcrumbs(string telemetry, string userID = null, bool isMyVI = true)
{
MyServiceAudit audit;
Guid UserID;
if (Guid.TryParse(userID, out UserID))
audit = InsertAudit(UserID);
else
audit = InsertAudit(null);
try
{
Models.Telemetry data = JsonConvert.DeserializeObject<Models.Telemetry>(telemetry);
Controllers.Telemetry.UpdateTelemetry(data, isMyVI);
}
catch (Exception ex)
{
MyServiceAuditDB.FailAudit(audit.ID, ex.Message + " " + ex.StackTrace);
}
}

我完全被难住了,我已经尝试用更大的最大值等更新服务器上的 web.config,以查看它在那一侧的截断情况。

我的 ajax 中唯一的其他区别是 3 分钟的全局超时。

这是简单到没有在客户端处理特殊字符,还是服务器端限制,或者数据太大以至于被分块而服务器端不知道等待下一个 block ?

最佳答案

在一些评论、答案的帮助下,最终得到了实时失败的数据,我得以解决这个问题。

事实证明,不仅用户使用了特殊字符,而且发送下来的一些静态数据也包括这个(GIGO - 无法相信我们某些数据的状态)。

客户端解决方案:

encodeURIComponent() 函数对 URI 组件进行编码。此函数对特殊字符进行编码。此外,它还对以下字符进行编码:,/? : @ & = + $ #

$.ajax(myURL("SendBreadcrumbs"), {
type: "POST",
cache: false,
data: { telemetry: encodeURIComponent(telemetry.JSONify()), userID: currentUser, isMyVI: isMyVI }
})

服务器端解决方案:

将字符串转换为其未转义的表示形式。 Uri.UnescapeDataString()

public void SendBreadcrumbs(string telemetry, string userID = null, bool isMyVI = true)
{
MyServiceAudit audit;
Guid UserID;
if (Guid.TryParse(userID, out UserID))
audit = InsertAudit(UserID);
else
audit = InsertAudit(null);
try
{
Models.Telemetry data = JsonConvert.DeserializeObject<Models.Telemetry>(Uri.UnescapeDataString(telemetry));
Controllers.Telemetry.UpdateTelemetry(data, isMyVI);
}
catch (Exception ex)
{
MyServiceAuditDB.FailAudit(audit.ID, ex.Message + " " + ex.StackTrace);
}
}

Web 应用程序配置:

由于尝试发送 20 封电子邮件,我有一个用户收到 500 错误。我更新了配置以包括最大请求长度(以千字节为单位,示例为 1GB)和最大内容长度(以字节为单位,示例为 1GB)。电子邮件没有问题。简直不敢相信!

<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>
<system.web>
<httpRuntime targetFramework="4.5" maxQueryStringLength="32768" maxUrlLength="65536" maxRequestLength="1048576" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxQueryString="32768" maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647" />
</webServices>
</scripting>
</system.web.extensions>

关于c# - 为什么我的 ajax 帖子被截断了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41898747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com