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

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

本文介绍了为什么我的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帖子被截断了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:22