【发布时间】:2020-06-18 00:29:53
【问题描述】:
我目前正在进行 ajax 调用以从 API 返回数据。返回值是一个数组的对象。此格式是无效对象,因此我在访问返回给我的数据时遇到问题。
如何将此对象转换为有效的 json 对象,以便访问返回的数据?
这里是一个返回数据的例子:
data = { ["<p>Retail Websites has a profit value of $243,291. In the year 2020 we have seen a growth rate of about 2.3% </p>" ] }
我尝试在我的 ajax 调用中使用dataType: json,但结果值仍然相同。
这是我使用“dataType: json”的 ajax 调用:
$.ajax({
type: "POST",
url: dataUrl,
data: {
"retailId": retailId,
},
dataType: "json",
async: true,
error: function () {
console.log("error")
}
}).done(function (data) {
console.log("retail data", data)
})
我无法访问此对象中的数据。我希望能够像 json 对象一样访问数据,例如:data[0]。我被困在如何做到这一点上。
【问题讨论】:
-
为什么 API 以无效的语法响应?
标签: javascript json ajax object