从单独的JS文件访问数据响应项(控制台错误)

时间:2018-10-27 22:20:52

标签: javascript

Object
response: "[{"id":"28","class_id":"1463","voluntary1":"1000","voluntary2":"1000","voluntary3":"1000","assigned_risk":"1500","state":"Alabama"}]"

我试图通过调用以下内容访问上方的voluntary3数据并将其追加到我的输入#input_52_22上:

document.getElementById('#input_52_22').value= +Object.response.voluntary3;

但是它响应以下控制台错误。

VM26293:1 Uncaught TypeError: Cannot read property 'voluntary3' of undefined
    at <anonymous>:1:65

下面是响应的屏幕截图;我只是想尝试自愿并附加作为我的输入ID值:

JS Bin code here

我正在努力从外部文件(原始的ajax成功调用之外执行此操作,可能是为什么?)

1 个答案:

答案 0 :(得分:3)

您记录的对象具有response属性,因此它是obj.response而不是Object.response,并且它是一个字符串,因此必须对其进行解析:

JSON.parse(/*obj*/.response).voluntary3
相关问题