You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native 0.55.0 + Axios偶现无效JSON返回问题求助

解决React Native 0.55.0 + Axios调用API偶尔收到无效JSON的问题

从你描述的情况来看,偶尔出现的键名异常(比如"ty"type_name")属于JSON格式损坏,结合服务端运行正常、偶尔能返回正确数据的现象,大概率是网络传输过程中的数据截断/粘包,或者老版本RN/Axios的兼容性问题导致的。下面给你具体的排查和解决思路:

一、先确认Axios的JSON解析逻辑

你提到resp.data返回的是字符串,正常情况下Axios会根据响应头的Content-Type: application/json自动将响应解析为JSON对象。如果偶尔返回字符串,可能是:

  • 服务端偶尔返回的响应头Content-Type不正确(比如变成了text/plain)
  • 网络传输中响应头被篡改,导致Axios未触发自动解析

建议在请求回调中打印响应头,排查是否存在异常,同时添加手动解析和错误捕获逻辑,方便定位问题:

export function getNotifications(params) {
  return (dispatch) => {
    dispatch({ type: SET_NOTIFICATIONS_LIST_REQUEST })
    return getNotificationsApi(params).then(resp => {
      // 打印响应头,确认Content-Type是否稳定
      console.log('响应头:', resp.headers);
      let data;
      try {
        // 强制手动解析,避免依赖Axios的自动处理
        data = typeof resp.data === 'string' ? JSON.parse(resp.data) : resp.data;
        console.log('解析后的数据:', data);
        dispatch(getNotificationsSuccess(data));
        return data;
      } catch (parseError) {
        // 关键:捕获解析错误并保存原始数据,方便后续分析
        console.error('JSON解析失败,原始错误数据:', resp.data);
        // 这里可以添加重试逻辑,比如重新请求一次
        return getNotificationsApi(params).then(retryResp => {
          const retryData = typeof retryResp.data === 'string' ? JSON.parse(retryResp.data) : retryResp.data;
          dispatch(getNotificationsSuccess(retryData));
          return retryData;
        }).catch(retryErr => {
          console.error('重试请求也失败:', retryErr);
          return Promise.reject(retryErr);
        });
      }
    }).catch(err => { 
      console.error('请求异常:', err);
    })
  }
}

二、排查网络层面的问题

移动端弱网环境下,TCP协议的分包/粘包可能导致数据拼接错误,出现类似"ty"type_name"这种截断后重复拼接的异常。你可以:

  • 在弱网环境下测试是否更容易复现问题
  • 给Axios配置合理的超时时间,避免请求在网络不稳定时长时间等待导致数据损坏:
// 在Axios实例配置中添加超时
const axiosInstance = axios.create({
  baseURL: '你的API地址',
  timeout: 10000, // 10秒超时
});

三、版本兼容性排查

你使用的React Native 0.55.0是比较老旧的版本(2018年发布),对应的Axios版本可能存在兼容性问题:

  • 尝试升级Axios到与RN 0.55.0兼容的稳定版本(比如axios@0.18.1,这是同期的稳定版)
  • 检查RN的网络模块是否有已知bug,比如老版本的XMLHttpRequest在处理大响应时的截断问题

四、验证服务端输出(辅助排查)

虽然你确认服务端运行正常,但可以让服务端开发人员在日志中记录每次返回的完整JSON内容,当客户端出现解析错误时,对比服务端日志和客户端收到的错误数据,确认是传输过程中损坏还是服务端偶尔输出异常。


附你提供的相关代码和正常数据:

Action文件代码

export function getNotifications(params):Action { 
  return (dispatch) => { 
    dispatch({ type: SET_NOTIFICATIONS_LIST_REQUEST }) 
    return getNotificationsApi(params).then(resp => { 
      var data = resp.data 
      alert(typeof data) //it's return string 
      console.log(data) 
      dispatch(getNotificationsSuccess(data)) 
      return resp.data 
    }).catch(err => { }) 
  } 
}

正常返回的JSON数据

{"data":[{"id":583,"type":"itemcomment","type_id":10036,"datetime":"2018-05-15T06:48:39","status":1,"user_id":8,"type_name":"test new","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"}},{"id":581,"type":"itemcomment","type_id":10036,"datetime":"2018-05-15T06:48:05","status":1,"user_id":8,"type_name":"test new","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"}},{"id":578,"type":"itemcomment","type_id":10036,"datetime":"2018-05-15T06:42:50","status":1,"user_id":8,"type_name":"test new","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"}},{"id":449,"type":"editoutfits","type_id":5,"datetime":"2018-05-09T10:48:15","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":430,"type":"edititem","type_id":10034,"datetime":"2018-05-08T07:59:29","status":1,"user_id":8,"type_name":"Item3","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"itemimg":[{"image":"1524568505IMG_0344.PNG"},{"image":"1525761327virtual_closet_mobile.jpg"}]},{"id":427,"type":"edititem","type_id":10034,"datetime":"2018-05-08T06:35:26","status":1,"user_id":8,"type_name":"Item3","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"itemimg":[{"image":"1524568505IMG_0344.PNG"},{"image":"1525761327virtual_closet_mobile.jpg"}]},{"id":424,"type":"editoutfits","type_id":5,"datetime":"2018-05-08T05:44:08","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":421,"type":"editoutfits","type_id":5,"datetime":"2018-05-08T05:38:41","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":418,"type":"editoutfits","type_id":5,"datetime":"2018-05-08T05:38:29","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":355,"type":"editoutfits","type_id":5,"datetime":"2018-05-03T05:44:22","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":353,"type":"edititem","type_id":10025,"datetime":"2018-05-02T12:03:54","status":1,"user_id":8,"type_name":"Item1","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"itemimg":[{"image":"1524201903virtual_closet_mobile.jpg"},{"image":"1524201903IMG_0190.PNG"}]},{"id":351,"type":"editoutfits","type_id":5,"datetime":"2018-05-02T05:48:26","status":1,"user_id":8,"type_name":"Test","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"oufitimg":"1525758248newlookImage.jpg"},{"id":283,"type":"addoutfits","type_id":8,"datetime":"2018-04-24T12:26:24","status":1,"user_id":6,"type_name":"Testttttt","user":{"username":"user2","id":"6","first_name":"sonam","last_name":"dogra"},"user_meta":{"value":"1524139232IMG_0186.PNG"},"oufitimg":"1524572784newlookImage.jpg"},{"id":250,"type":"additem","type_id":10034,"datetime":"2018-04-24T11:15:05","status":1,"user_id":8,"type_name":"Item3","user":{"username":"user4","id":"8","first_name":"ruhi","last_name":"vohra"},"user_meta":{"value":"1524139730IMG_0189.JPG"},"itemimg":[{"image":"1524568505IMG_0344.PNG"},{"image":"1525761327virtual_closet_mobile.jpg"}]},{"id":235,"type":"editoutfits","type_id":3,"datetime":"2018-04-24T10:18:03","status":1,"user_id":8,"type_name":"Outfit1"}]}

内容的提问来源于stack exchange,提问作者pallav test

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:31:45