javascript - 使用 AJAX XMLHttpRequest 时,Symfony StreamedResponse 的响应文本被连接起来

标签 javascript ajax symfony xmlhttprequest

我下面有一个 Controller ,它会在端点被调用时立即返回 Line 1(作为响应)。两秒后它返回 Line 2。当我直接访问 URL http://ajax.dev/app_dev.php/v2 时这很好,所以这证明端点按预期工作。

/**
 * @Method({"GET"})
 * @Route("/v2", name="default_v2")
 *
 * @return Response
 */
public function v2Action()
{
    $response = new StreamedResponse();
    $response->setCallback(function () {
        echo 'Line 1';
        ob_flush();
        flush();

        sleep(2);
        echo 'Line 2';
        ob_flush();
        flush();
    });

    return $response;
}

当我使用 AJAX 调用同一个端点时,第一个响应很好,它是 response: "Line 1"。但是,第二个是 response: "Line 1Line2" 所以它是合并的。我应该怎么做才能获得 response: "Line2" 作为第二个响应?请参阅下面的控制台日志。

XMLHttpRequest { onreadystatechange: xhr.onreadystatechange(), readyState: 3,
timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload,
responseURL: "http://ajax.dev/app_dev.php/v2", status: 200, 
statusText: "OK", responseType: "", response: "Line 1" }

XMLHttpRequest { onreadystatechange: xhr.onreadystatechange(), readyState: 3,
timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload,
responseURL: "http://ajax.dev/app_dev.php/v2", status: 200, 
statusText: "OK", responseType: "", response: "Line 1Line2" }

Complete

这是我正在使用的 AJAX。

$(document).ready(function () {
    $('button').click(function () {
        xhr = new XMLHttpRequest();
        xhr.open("GET", 'http://ajax.dev/app_dev.php/v2', true);
        xhr.onprogress = function(e) {
            console.log(e.currentTarget);
        };
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                console.log("Complete");
            }
        };
        xhr.send();
    });
});

最佳答案

目前这只能是一个临时解决方案,因为每个单独的响应加起来会变成一个巨大的响应,而不是一个干净的响应。

例如

Response 1: Hello
Response 2: World
Response 3: Bye

XMLHttpRequest e.currentTarget 属性将包含:

Hello
HelloWorld
HelloWorldBye

The responseText property of XMLHttpRequest always contains the content that's been flushed out of the server, even when the connection's still open. So the browser can run a periodic check, e.g. to see if its length has changed.

暂时我会使用下面的代码,但如果我能找到更好的解决方案,我会发布它。

<script>
    $(document).ready(function () {
        $('button').click(function () {
            xhr = new XMLHttpRequest();
            xhr.open('GET', 'http://ajax.dev/app_dev.php/v2', true);
            xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

            xhr.onprogress = function(e) {
                var response = e.currentTarget.response;
                var output = typeof lastResponseLength === typeof undefined
                    ? response
                    : response.substring(lastResponseLength);

                lastResponseLength = response.length;
                console.log(output);
            };

            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    console.log('Complete');
                }
            };

            xhr.send();
        });
    });
</script>

关于javascript - 使用 AJAX XMLHttpRequest 时,Symfony StreamedResponse 的响应文本被连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43684655/

相关文章:

jquery - 更改为 https ajax 功能后无法正常工作

php - 'Twig_Error_Syntax' 消息未知 "form_start"函数

php - 错误:预期的 Doctrine\ORM\Query\Lexer::T_WITH,得到 'ON'

javascript - 使用 AngularJS 中的按钮迭代 <option> 值

javascript - 直接从浏览器中的输入将 Assets 上传到 contentful (sdk CMA) 时出错

javascript - 映射时嵌套数据表

javascript - Javascript While 循环中的意外输出

javascript - 在表单中的操作之前执行 ajax

javascript - 使用Ajax添加TinyMCE实例-Grails

php - 使用 doctrine symfony 清除只有一个实体/存储库的结果缓存