导航期间 jQuery ajax 调用的 MVC 操作中的空参数(仅限 IE)

发布于 2024-12-24 03:42:53 字数 1025 浏览 7 评论 0原文

我是一个 MVC/ajax 项目的程序员,该项目从客户那里获得了大量流量。我们(每天)看到一两个孤立的控制器操作实例未从客户端接收参数。

长话短说:动作是通过 jQuery ajax 调用的,并且只有在浏览器导航到另一个页面时进行 ajax 调用时,动作参数才为 null。 IE 单击一个链接,然后触发 ajax 调用。

我在这些调用周围添加了一些粗略的验证,以确保我们实际上没有在 ajax 数据中传递空值,但这并没有缓解问题。下面是其中一个调用的示例。

    var searchValue = _txtSearch.val().trim();

    if (searchValue === null 
        || searchValue === undefined 
        || searchValue.length < _minimumLengthForSearch) {
        _txtSearch.focus();
        return;
    }
    // clear out the value when launching
    _txtSearch.val('');

    $.post(_quickSearchUrl,
        { searchString: searchValue },
        function (data) {...},
    "json");

我发现一篇旧的 IEBlog 帖子 这表明 IE 处理这种情况的方式可能与其他浏览器不同。我很好奇以前是否有人遇到过这种现象。同样,我只能在 IE 中重现此问题,并且只能在页面导航期间重现。

编辑:由于某种原因,在 Fiddler 处于活动状态时很难重现此异常,但是当我设法 Fiddler 显示以下错误消息:

Fiddler 在会话 #4 中检测到协议违规。 内容长度不匹配:请求标头指示 24 字节,但客户端发送了 0 字节。

I am a programmer on an MVC/ajax project that receives significant traffic from its customers. We've been seeing one or two isolated instances (per day) of a controller action not receiving parameters from the client.

Long story short: the actions are being called via jQuery ajax, and the action params are only null if the ajax call is made while the browser is navigating to another page. IE click a link and then trigger an ajax call.

I added some crude validation around these calls to ensure that we aren't actually passing nulls in the ajax data, and this hasn't alleviated the problem. An example of one of the calls is below.

    var searchValue = _txtSearch.val().trim();

    if (searchValue === null 
        || searchValue === undefined 
        || searchValue.length < _minimumLengthForSearch) {
        _txtSearch.focus();
        return;
    }
    // clear out the value when launching
    _txtSearch.val('');

    $.post(_quickSearchUrl,
        { searchString: searchValue },
        function (data) {...},
    "json");

I found an old IEBlog post that suggests IE might handle this situation differently from other browsers. I was curious as to whether anyone else has encountered this phenomena before. Again, I can only reproduce this issue in IE and only during page navigation.

Edit: It is difficult to reproduce this exception with Fiddler active for some reason, but when I manage to Fiddler displays the following error message:

Fiddler has detected a protocol violation in session #4.
Content-Length mismatch: Request Header indicated 24 bytes, but client sent 0 bytes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2024-12-31 03:42:53

使用 Fiddler,我能够在非常罕见的情况下重现此情况,并意识到它可以被视为内容长度不匹配(即发生时 fiddler 显示的错误消息)。具体来说,在服务器端,请求内容长度将与实际的 Form/InputStream 内容不匹配。我们重写 OnAuthorization 来手动检测和处理这种情况。

    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        //Detect IE missing post data phenomenon
        var request = HttpContext.Request;
        if (request.IsAjaxRequest() == true && request.ContentLength > 0 
            && request.Form.HasKeys() == false && request.InputStream.Length == 0)
        {
            throw new ContentLengthMismatchException(string.Format("Content Length Mismatch in URL: {0}", request.Url));
        }

        base.OnAuthorization(filterContext);
    }

Using Fiddler, I was able to reproduce this in a very rare instance and realized that it can be treated as a content length mismatch (that is the error message that fiddler displays when it occurs). Specifically, on the server side the request content length will not match the actual Form/InputStream contents. We overrode OnAuthorization to manually detect and handle this case.

    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        //Detect IE missing post data phenomenon
        var request = HttpContext.Request;
        if (request.IsAjaxRequest() == true && request.ContentLength > 0 
            && request.Form.HasKeys() == false && request.InputStream.Length == 0)
        {
            throw new ContentLengthMismatchException(string.Format("Content Length Mismatch in URL: {0}", request.Url));
        }

        base.OnAuthorization(filterContext);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文