【发布时间】:2020-06-24 22:32:42
【问题描述】:
我正在尝试使用 ajax 和 'GET' 方法从 API url 显示 JSON。
function getLatestComments() {
$.ajax({
type: 'GET',
url: 'https://***********-API.amazonaws.com/deploymentstage/ride',
headers: {
Authorization: authToken
},
contentType: 'application/json',
success: function (data) {
var text = $("body").append(JSON.stringify(data.records));
console.log(text);
text.forEach(function(records) {
$('#messages').append ('<p> +comments + </p>')
});
},
error: function(err) {
console.error('Something went wrong: ', err);
}
});
}
来自 URL 的 JSON:
statusCode 200
records
0 "\"Lets give it a try\""
1 "\"this time its west\""
2 "\"Abcdef\""
3 "\"this is another test\""
headers
Access-Control-Allow-Origin "*"
【问题讨论】:
-
让我再发布一次我正在尝试的功能..
-
contentType在GET请求中不是必需的,因为您没有发送任何内容。 -
什么是
comments? -
text.forEach()没有多大意义。您正在循环遍历$("body")的元素,而不是 JSON 中的记录。 -
你可能想要的是
data.records.forEach(...)
标签: javascript jquery arrays ajax