为什么我的Ajax帖子被截断了?

编程入门 行业动态 更新时间:2024-10-24 07:28:25
本文介绍了为什么我的Ajax帖子被截断了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚刚更新了我的mvc服务,以包含更大的错误和日志记录.我现在已经多次遇到这个确切的错误.但是无法复制.

I have just updated my mvc service to include greater error and logging. I have now got this exact error several times. But cannot replicate.

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

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

The Path is different each time, depending on what the user is sending to the server.

我的ajax请求通常如下所示:

My ajax requests generally look like this:

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

在这些情况下,不存在userID和isMyVI(布尔值),并且遥测字符串被截断了.

In these cases, userID and isMyVI (boolean) didnt exist and the telemetry string is truncated.

JSONify方法如下:

JSONify method is as follows:

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

这是淘汰赛JS的序列化器.

This is knockoutJSs serializer.

服务器端:

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); } }

我完全迷住了,我尝试用更大的maxvalues等更新服务器上的web.config,以查看它在那端被截断了.

I am completely stumped, i have tried updating the web.config on the server with larger maxvalues etc, to see its truncating on that side.

我的ajax唯一的不同之处是全局超时为3分钟.

The only other difference i have in my ajax is a global timeout of 3mins.

这仅仅是因为没有在客户端处理特殊字符,还是服务器端限制,还是数据太大而被分块并且服务器端不知道等待下一个块?

Is this as simple as special characters not being handled on client side, or server side limitations, or is the data so large it gets chunked and server side doesn't know to wait for the next chunk?

推荐答案

在一些评论,答案的帮助下,最后获得了实时失败的数据,我得以解决此问题.

With help from a few comments, answers and finally got live failed data, I was able to resolve this.

事实证明,用户不仅使用特殊字符,而且还发送了一些静态数据(GIGO-无法相信我们某些数据的状态).

It turned out that not only did the user use special characters, but also some of the static data sent down included this (GIGO - couldn't believe the state of some of our data).

客户端解决方案:

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

The encodeURIComponent() function encodes a URI component. This function encodes special characters. In addition, it encodes the following characters: , / ? : @ & = + $ #

$.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).电子邮件没有问题.真不敢相信!

I had a user getting a 500 error, due to trying to send 20 emails. I updated the config to include maximum request length (in kilobytes, example is 1GB) and max content length (in bytes, example is 1GB). Emails came through no issue. Couldn't believe it!

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

更多推荐

为什么我的Ajax帖子被截断了?

本文发布于:2023-10-17 13:10:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1501014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帖子   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!