简体   繁体   中英

Null parameters in MVC action from jQuery ajax call during navigation (IE only)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM