Ajax POST 对 Heroku 的请求丢失 post 数据

Ajax POST requests to Heroku loses post data

我对 Heroku 应用程序的 ajax post 请求在大约 90% 的调用中丢失了其 post 数据。

出于安全原因,我使用的是 GET 并更改为 POST。 GET 请求已经工作了几个月。 POST 请求在 IDE 本地工作,并使用工头。但是,当将其移至我的 Heroku 应用程序时,POST 数据在 90% 的调用中丢失。

我很确定数据已正确发送为:

  1. 它在 运行 我的本地应用程序
  2. 时有效
  3. 使用 Chrome 开发工具验证数据是调用的一部分

我将详细的传入请求数据转储到 Papertrail 日志中。在这里我可以看到数据不见了。 content-length 表示请求数据应该在那里,但它只是没有了。

我正在使用的 Ajax 调用。我试过 $.post 请求和 non-jquery ajax 请求。我在数据上尝试了 JSON.stringify 和许多不同的请求组合 headers。

 function doPostRequestWithDataV2(method, data) {
    var methodName = "/call/echo";
    var jqxhr = $.ajax({
        type: "POST",
        contentType: "application/json;charset=utf-8",
        url: methodName,
        data: {uuuuu: "ppppp"},
    })
            .done(function () {
                var jsonString = jqxhr.responseText;
                alert("All went well: " + jsonString);
            })
            .error(function () {
                alert("Problem: " + jqxhr.responseText);
            });
}

完整输出调用成功。重要差异以粗体显示。

Mar 07 07:29:18 my-fine-app app/web.1: FIRST TOUCH: Number of bytes 945
Mar 07 07:29:18 my-fine-app app/web.1: FIRST TOUCH: ##########
Mar 07 07:29:18 my-fine-app app/web.1: POST /call/echo HTTP/1.1
Mar 07 07:29:18 my-fine-app app/web.1: Host: staging.kollektivafondval.se
Mar 07 07:29:18 my-fine-app app/web.1: Connection: close
Mar 07 07:29:18 my-fine-app app/web.1: User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:36.0) Gecko/20100101 Firefox/36.0
Mar 07 07:29:18 my-fine-app app/web.1: Accept: */*
Mar 07 07:29:18 my-fine-app app/web.1: Accept-Language: en-US,en;q=0.5
Mar 07 07:29:18 my-fine-app app/web.1: Accept-Encoding: gzip, deflate
Mar 07 07:29:18 my-fine-app app/web.1: Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Mar 07 07:29:18 my-fine-app app/web.1: X-Requested-With: XMLHttpRequest
Mar 07 07:29:18 my-fine-app app/web.1: Referer: http://staging.kollektivafondval.se/debug/index.html
Mar 07 07:29:18 my-fine-app app/web.1: Cookie: _ga=GA1.2.492366677.1421911589; __utma=33200548.492366677.1421911589.1425495130.1425495130.1; __utmc=33200548; __utmz=33200548.1425495130.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); cp=0; undefined=0; ptl=0; _gat=1
Mar 07 07:29:18 my-fine-app app/web.1: Pragma: no-cache
Mar 07 07:29:18 my-fine-app app/web.1: Cache-Control: no-cache
Mar 07 07:29:18 my-fine-app app/web.1: X-Request-Id: 17981145-acce-49bf-a5dd-e212a2a184ef
Mar 07 07:29:18 my-fine-app app/web.1: X-Forwarded-For: 83.227.55.31
Mar 07 07:29:18 my-fine-app app/web.1: X-Forwarded-Proto: http
Mar 07 07:29:18 my-fine-app app/web.1: X-Forwarded-Port: 80
Mar 07 07:29:18 my-fine-app app/web.1: Via: 1.1 vegur
Mar 07 07:29:18 my-fine-app app/web.1: Connect-Time: 0
Mar 07 07:29:18 my-fine-app app/web.1: X-Request-Start: 1425709758119
Mar 07 07:29:18 my-fine-app app/web.1: Total-Route-Time: 0
Mar 07 07:29:18 my-fine-app app/web.1: Content-Length: 17
Mar 07 07:29:18 my-fine-app app/web.1: String=any+string
Mar 07 07:29:18 my-fine-app app/web.1: ##########

数据丢失时完整输出。重要差异以粗体显示。

Mar 07 07:29:15 my-fine-app app/web.1: ************************************************************
Mar 07 07:29:16 my-fine-app app/web.1: FIRST TOUCH: Number of bytes 928
Mar 07 07:29:16 my-fine-app app/web.1: FIRST TOUCH: ##########
Mar 07 07:29:16 my-fine-app app/web.1: POST /call/echo HTTP/1.1
Mar 07 07:29:16 my-fine-app app/web.1: Host: staging.kollektivafondval.se
Mar 07 07:29:16 my-fine-app app/web.1: Connection: close
Mar 07 07:29:16 my-fine-app app/web.1: User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:36.0) Gecko/20100101 Firefox/36.0
Mar 07 07:29:16 my-fine-app app/web.1: Accept: */*
Mar 07 07:29:16 my-fine-app app/web.1: Accept-Language: en-US,en;q=0.5
Mar 07 07:29:16 my-fine-app app/web.1: Accept-Encoding: gzip, deflate
Mar 07 07:29:16 my-fine-app app/web.1: Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Mar 07 07:29:16 my-fine-app app/web.1: X-Requested-With: XMLHttpRequest
Mar 07 07:29:16 my-fine-app app/web.1: Referer: http://staging.kollektivafondval.se/debug/index.html
Mar 07 07:29:16 my-fine-app app/web.1: Cookie: _ga=GA1.2.492366677.1421911589; __utma=33200548.492366677.1421911589.1425495130.1425495130.1; __utmc=33200548; __utmz=33200548.1425495130.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); cp=0; undefined=0; ptl=0; _gat=1
Mar 07 07:29:16 my-fine-app app/web.1: Pragma: no-cache
Mar 07 07:29:16 my-fine-app app/web.1: Cache-Control: no-cache
Mar 07 07:29:16 my-fine-app app/web.1: X-Request-Id: 16caf2b0-88a6-455b-ac03-71581920e1e5
Mar 07 07:29:16 my-fine-app app/web.1: X-Forwarded-For: 83.227.55.31
Mar 07 07:29:16 my-fine-app app/web.1: X-Forwarded-Proto: http
Mar 07 07:29:16 my-fine-app app/web.1: X-Forwarded-Port: 80
Mar 07 07:29:16 my-fine-app app/web.1: Via: 1.1 vegur
Mar 07 07:29:16 my-fine-app app/web.1: Connect-Time: 0
Mar 07 07:29:16 my-fine-app app/web.1: X-Request-Start: 1425709756264
Mar 07 07:29:16 my-fine-app app/web.1: Total-Route-Time: 0
Mar 07 07:29:16 my-fine-app app/web.1: Content-Length: 17
Mar 07 07:29:16 my-fine-app app/web.1: ##########

嗯。我找到了我自己问题的答案。我发现 Heroku 的路由层在发送 header 后会暂停。所以我所要做的就是在读取所有字节后添加 2 毫秒的睡眠,然后再从套接字中读取。现在它完美地工作。