首页 > web前端 > js教程 > 正文

Axios请求返回arraybuffer,如何修改接口使其返回JSON数据?

聖光之護
发布: 2025-02-20 23:04:23
原创
285人浏览过

axios请求返回arraybuffer,如何修改接口使其返回json数据?

如何修改Axios接口使其返回JSON数据而非ArrayBuffer

本文介绍如何修改后端接口,使其返回JSON数据,而不是使用Axios时返回的ArrayBuffer。假设您使用Axios发送GET请求并接收ArrayBuffer响应,但希望接口返回JSON格式的数据。 关键在于修改服务器端代码,而不是客户端Axios配置。

一、修改服务器端接口代码:

以下示例展示如何修改一个Node.js (koa) 后端接口,使其返回JSON数据:

原接口(返回ArrayBuffer):

router.post('/a/b.zip', async ctx => {
  const filepath = path.join(__dirname, ctx.req.url.replace('/a', ''));
  const buf = fs.readFileSync(filepath);
  ctx.set('content-type', 'application/octet-stream'); // or other appropriate content-type
  ctx.status = 200;
  ctx.body = buf; // Returns ArrayBuffer
});
登录后复制

修改后的接口(返回JSON):

router.post('/a/b.zip', async ctx => {
  const filepath = path.join(__dirname, ctx.req.url.replace('/a', ''));
  const buf = fs.readFileSync(filepath);
  // 将ArrayBuffer转换为可JSON化的格式 (例如Base64编码)
  const base64Data = buf.toString('base64'); 

  ctx.set('content-type', 'application/json');
  ctx.status = 200;
  ctx.body = JSON.stringify({ data: base64Data }); // Returns JSON
});
登录后复制

关键修改在于:

  1. 将ctx.set('content-type', 'application/octet-stream');改为ctx.set('content-type', 'application/json');
  2. 将ctx.body = buf;改为ctx.body = JSON.stringify({ data: base64Data });,其中base64Data是将buf转换为Base64编码后的字符串。 选择合适的编码方式取决于你的数据类型和需求。

二、客户端Axios代码 (无需修改):

因为我们修改了服务器端返回JSON,所以客户端Axios代码不需要更改responseType。 它会自动解析JSON响应。

三、完整示例 (Node.js Koa + Axios):

服务器端 (Koa):

const Koa = require('koa');
const Router = require('koa-router');
const fs = require('node:fs');
const path = require('node:path');

const app = new Koa();
const router = new Router();

router.post('/a/b.zip', async ctx => {
  const filepath = path.join(__dirname, ctx.req.url.replace('/a', ''));
  const buf = fs.readFileSync(filepath);
  const base64Data = buf.toString('base64'); 
  ctx.set('content-type', 'application/json');
  ctx.status = 200;
  ctx.body = JSON.stringify({ data: base64Data }); 
});

app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);
登录后复制

客户端 (Axios):

axios.post('/a/b.zip')
  .then(response => {
    console.log(response.data); // 解析JSON数据
    const decodedData = Buffer.from(response.data.data, 'base64'); // 解码Base64
    // ...处理decodedData...
  })
  .catch(error => {
    console.error(error);
  });
登录后复制

记住根据你的后端框架和数据类型调整代码。 例如,如果你使用的是Express.js,ctx将被替换成res。 你可能还需要调整Base64编码或使用其他编码方式,例如Uint8Array。 确保服务器端和客户端的编码方式一致。

以上就是Axios请求返回arraybuffer,如何修改接口使其返回JSON数据?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号