简体   繁体   English

jQuery Ajax发布请求在Chrome上暂停

[英]jQuery Ajax post request stuck on pending on Chrome

I've got a jQuery client making an Ajax post request to a Spring Controller. 我有一个jQuery客户端向Spring Controller发出Ajax发布请求。 On the server side, no error has been logged. 在服务器端,未记录任何错误。 On the client side, the request is stuck on pending a very long time, minutes, then might fail either with ERR_SPDY_PROTOCOL_ERROR or ERR_CONNECTION_CLOSED. 在客户端,请求将持续很长时间(分钟),然后可能会因ERR_SPDY_PROTOCOL_ERROR或ERR_CONNECTION_CLOSED而失败。

This problem is reproducible on Chrome, but not on Firefox. 此问题在Chrome上可重现,但在Firefox上不可重现。 Verified affected version is 70.0.3538.77, there may be others too. 已验证的受影响的版本是70.0.3538.77,也可能有其他版本。 Also, the problem occurs on a specific deployment of the application and not elsewhere, development or production. 此外,问题发生在应用程序的特定部署上,而不是其他地方,开发或生产。

The client sends HTTPS 2 requests on that environment. 客户端在该环境上发送HTTPS 2请求。 On the development environment it is HTTP 1.1. 在开发环境中,它是HTTP 1.1。 On the server, all requests are recorded as 1.1. 在服务器上,所有请求都记录为1.1。

For no apparent reason, the requests started going through, but this is a recurring problem and would like to solve it. 没有明显的原因,请求开始通过,但这是一个反复出现的问题,并希望解决它。 Since the problem started happening, I can't reproduce it and check if the problem is too many connections to the server (more than 6). 由于问题开始发生,我无法重现它并检查问题是否与服务器连接太多(超过6个)。 I use three DNS servers, the last one of which is Google's 8.8.8.8. 我使用三台DNS服务器,其中最后一台是Google的8.8.8.8。

I am looking for a code fix or a hint whether this could be related to server setup. 我正在寻找代码修复或提示是否这可能与服务器设置有关。 I am almost certain that it is a combination of client code and networking. 我几乎可以肯定它是客户端代码和网络的组合。

What the problem is not: 问题不在于:

What I've tried unsuccessfully: 我尝试过的失败了:

What doesn't answer the question: 什么不回答这个问题:

On the client side, I've tried clearing browser data, flushing sockets and private browsing / incognito. 在客户端,我已经尝试清除浏览器数据,刷新套接字和隐私浏览/隐身。

The only thing that sometimes, rarely, bypasses the error is going on incognito and flushing sockets and emptying cache from chrome://net-internals/#events 有时,很少会绕过错误的唯一事情就是隐身和刷新套接字并从chrome:// net-internals /#events中清空缓存

var formData = new FormData();

formData.append( /* ... */ );

//...

$.ajax({
    type: "POST",
    url: "/somepath/update",
    cache: false,
    data: formData,
    contentType: false,
    processData: false,
    success: function(result) {
        //...
    },
    fail: function(result) {
        //....
    },
    error: function( jqXHR, textStatus, errorThrown ){
        alert(textStatus + ":" + errorThrown);
    }
});

A separate request using $.post was going through: 使用$ .post的单独请求正在通过:

$.post("someotherpath/update", $("#someForm").serialize())
    .done(function (data) {
        //...
     })
     .fail(function (data) {
         //...
     })
     .always(function () {
         //...
     });

Server side: 服务器端:

@RequestMapping(value="/somepath/update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public @ResponseBody String update(ModelClass model) {
    JSONObject result = new JSONObject();
    //...
    return result.toString();
}

If it's relevant, there are the following filters. 如果相关,则有以下过滤器。 I'm afraid I can't post more of them at the moment: 我恐怕此刻不能发布更多内容:

@Configurable
public class Filter1 extends OpenEntityManagerInViewFilter implements Filter{

    public void doFilterInternal(HttpServletRequest httpReq, HttpServletResponse httpResp, FilterChain chain)
        throws ServletException, IOException {
        //...
    }
}


@Configurable
public class Filter2 extends OncePerRequestFilter implements Filter{

    public void doFilterInternal(HttpServletRequest httpReq, HttpServletResponse httpResp, FilterChain chain)
        throws ServletException, IOException {
        //...
    }
}

@Order(/* very small integer */)
public class Filter3 extends OncePerRequestFilter {

}

Expected result is that the code should go through the success callback. 预期的结果是代码应该通过成功回调。 Instead the request is stuck on pending for minutes, then enters the error callback. 相反,请求会暂停等待几分钟,然后输入错误回调。

$.post is calling $.ajax under the covers just defaulting several of the options. $.post正在调用$.ajax ,只是默认了几个选项。

$.post defaults contentType to "application/x-www-form-urlencoded; charset=UTF-8" which would be consistent with your serialized form data payload. $.post contentType默认为"application/x-www-form-urlencoded; charset=UTF-8" ,这与您的序列化表单数据有效负载一致。

Your call to $.ajax sets contentType to false -- which may cause the browser to send a pre-flight OPTIONS request to the server, which may be causing the difference in behavior you're experiencing. 您对$.ajax调用将contentType设置为false - 这可能导致浏览器向服务器发送飞行前OPTIONS请求,这可能导致您遇到的行为不同。

I would suggest reading the details on jQuery.ajax() and the various behaviors based on options passed here 我建议阅读jQuery.ajax()的详细信息以及基于此处传递的选项的各种行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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