解决后端返回数据大于16位出现精度丢失问题

问题:

日志打印的数据:

 实际数据:

 解决方案:

 1、安装json-bigint:

npm i json-bigint

2、引入使用

复制代码
import axios from 'axios'
import JSONbig from 'json-bigint'
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';
// 创建axios实例
const service = axios.create({
  // axios中请求配置有baseURL选项,表示请求URL公共部分
  baseURL: process.env.VUE_APP_BASE_API,
  // 超时
  timeout: 60000,
  withCredentials: true,
  // 解决出现精度丢失处理部分
  transformResponse: [
    function (data) {
      if (typeof data === 'string') {
        try {
          data =JSONbig.parse(data); 
     }
catch (e) { /* Ignore */ } } return data; } ] });
复制代码

3、在uniapp中使用 json-bigint 处理:

设置dataType为text:

1
dataType: 'text'

在数据返回处处理:

 Vue.prototype.$u.http.interceptor.response = (res) => {
    // 解决出现数据精度缺失问题
    const jsonBigint = require('json-bigint')({ storeAsString: true });
    res = jsonBigint.parse(res.data);
}

以上两种处理方式是我分别用于处理uniapp和axios的方式,都能够把后台返回的精度丢失数据转为字符串来处理。

 

posted @   zaijinyang  阅读(1169)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示