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

如何从AJAX响应中获取ResCode?取值返回undefined该如何解决

问题根因

你接口返回的data.data.response是JSON格式的字符串,不是可直接读取属性的JavaScript对象,直接对字符串取属性自然会返回undefined。你可以先执行console.log(typeof data.data.response)验证,输出结果会是string。

解决方法

先调用JSON.parse()方法把这个字符串解析为JavaScript对象,再读取对应字段即可,修改后的代码如下:

success: function (data) {
  // 解析response字符串为对象
  const responseData = JSON.parse(data.data.response);
  // 读取目标字段
  console.log(responseData.ResStatus);
  console.log(responseData.ResCode);
},

可选优化

如果接口可能返回异常格式的字符串,建议加个try-catch捕获解析错误,避免代码直接报错:

success: function (data) {
  try {
    const responseData = JSON.parse(data.data.response);
    console.log(responseData.ResStatus);
    console.log(responseData.ResCode);
  } catch (e) {
    console.error('response字段解析失败', e);
  }
},

内容的提问来源于stack exchange,提问作者Hamza Qureshi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 11:39:04