【问题标题】:HTTP response truncated in browser浏览器中的 HTTP 响应被截断
【发布时间】:2018-10-11 20:54:29
【问题描述】:

我正在使用 axios 发出 HTTP 发布请求。响应的主体是一个 4MB 的大字符串。

axios({
    method: 'POST',
    url: url,
    data: data,
    headers : headers,
})
.then(function (response) {
    console.log(response);
});

但是,当我从浏览器调用此函数时,响应被截断为 1024 个字符(完整的响应位于“网络”选项卡中)。

另外,当我在终端中使用 Node 运行这个函数时,我得到了完整的响应。

从浏览器运行时,如何在我的 JavaScript 代码中获得完整响应?

【问题讨论】:

  • 你确定它不仅仅是console.log 截断.. 你试过console.log(response.length) 吗?您还尝试了设置timeout: 5000,也许maxContentLength: 5000000

标签: javascript npm vue.js axios


【解决方案1】:

为什么你在发布数据后会收到 4MB 的回复?

除此之外,如果你在nodeJS端,只需将响应的内容写入调试文件

const fse = require('fs-extra')

const postMyAwesomeData = async function () {
    try {
        const firstCall = await axios({ method: 'POST', url, data, headers });
        // if you need more of those calls
        const responses = Promise.all([firstCall]);

        // responses.forEach does not handle very well async/await ^^
        for (let [index, response] of responses.entries()) {
            await fse.write(`<path to debug file>/response${index}.txt`)
        }
    } catch (err) {
        console.log(`Hum something weird happen -_- ${err}`);
    }
}

看看这个很棒的包https://www.npmjs.com/package/fs-extra

但老实说,在发布数据后检索如此大的响应的实际用例是什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 2014-05-15
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多