在Axios中禁用JSON解析

LuL*_*eBe 8 javascript json axios

Ich有一个反应应用程序,我希望用户能够上传代码文件,然后他可以在网站上查看.很自然地,.json文件也被接受了.现在要获取文件内容,我使用axios对服务器上的文件发出get请求.

这适用于除JSON文件之外的所有内容,JSON文件会自动解析,因此不能作为String使用,而是作为javascript对象使用.再次转换为字符串,JSON.stringify删除所有换行符,所以我不能这样做.

有没有办法阻止axios自动解析JSON?

use*_*421 11

LuleBes的答案对我没有用.工作是什么: transformResponse: (req) => { return res; }, 如:

    axios.get(url, {
        headers,
        transformResponse: (res) => {
            // Do your own parsing here if needed ie JSON.parse(res);
            return res;
        },
        responseType: 'json'
    }).then(response => {
        // response.data is an unparsed string
    });
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用。简写是:`transformResponse: res => res` (3认同)

Jos*_*ush 6

设置以下选项以强制 axios 不解析响应:

transformResponse: x => x
Run Code Online (Sandbox Code Playgroud)

用法:

transformResponse: x => x
Run Code Online (Sandbox Code Playgroud)

现在response.data是 astring和以前一样object