我正在通过测试Nike API以获取有关任何产品的信息,供个人使用以了解Ajax和jQuery的小脚本。
我做了一些循环,以获取有关实际API的信息,但是当我想传递秒对象时,我陷入了困境
function getSnkrs() {
var test = $('.test');
$.ajax({
type: "GET",
url: "https://api.nike.com/product_feed/threads/v2/?filter=marketplace%28FR%29&filter=language%28fr%29&filter=channelId%28010794e5-35fe-4e32-aaff-cd2c74f89d61%29&&filter=exclusiveAccess%28true%2Cfalse%29",
async: false,
success: function(data) {
for (i = 0; i < 10; i++) {
for (y = 0; y < 10; y++) {
//<br><img src="'+ data._embedded.objects[i].publishedContent[i].nodes[i].properties[i].portraitURL[i].url +'"style="width: 20%; height=auto;><br>
test.append('<br><a>'+ data.objects[i].publishedContent[y].marketplace +'</a><br>');
}
}
}
});
}
在控制台上,我有这个:
jquery-1.9.1.js:7985未捕获的TypeError:无法读取未定义的属性“ marketplace”
在Object.success(script.js:36)
着火(jquery-1.9.1.js:1037)
at Object.fireWith [as resolveWith](jquery-1.9.1.js:1148)
完成时(jquery-1.9.1.js:8074)
在回调时(jquery-1.9.1.js:8598)
在Object.send(jquery-1.9.1.js:8604)
在Function.ajax(jquery-1.9.1.js:7978)
在getSnkrs(script.js:26)
在HTMLDocument。 (script.js:3)
起火(jquery-1.9.1.js:1037)
如果有人可以帮助我,我希望拥有“市场”元素,谢谢! :
{
"pages": {
"prev": "",
"next": "/product_feed/threads/v2?filter=marketplace%28FR%29&filter=language%28fr%29&filter=channelId%28010794e5-35fe-4e32-aaff-cd2c74f89d61%29&filter=exclusiveAccess%28true%2Cfalse%29&anchor=50",
"totalPages": 26,
"totalResources": 1258
},
"objects": [
{
"id": "a6ecf9f2-8f73-4d1a-b761-5e0772153fdb",
"channelId": "010794e5-35fe-4e32-aaff-cd2c74f89d61",
"channelName": "SNKRS Web",
"marketplace": "FR",
"language": "fr",
"lastFetchTime": "2019-08-13T15:13:15.879Z",
"publishedContent": {
"preview": false,
"marketplace": "FR",
答案 0 :(得分:3)
我看到PublisherContent不是数组? (如果我错了,请纠正我),它是一个json对象。尝试删除“ y”循环:
for (var i = 0; i < data.objects.length; i++) {
//<br><img src="'+ data._embedded.objects[i].publishedContent[i].nodes[i].properties[i].portraitURL[i].url +'"style="width: 20%; height=auto;><br>
test.append('<br><a>'+ data.objects[i].publishedContent.marketplace +'</a><br>');
}
答案 1 :(得分:1)
这是没有jQuery的答案,只是纯JS:
const marketplacesEl = document.getElementById("marketplaces");
fetch(
"https://api.nike.com/product_feed/threads/v2/?filter=marketplace%28FR%29&filter=language%28fr%29&filter=channelId%28010794e5-35fe-4e32-aaff-cd2c74f89d61%29&&filter=exclusiveAccess%28true%2Cfalse%29"
)
.then(response => response.json())
.then(({ objects }) =>
objects.forEach(
({ publishedContent: { marketplace } }) =>
(marketplacesEl.appendChild(
document.createElement("li")
).innerHTML = marketplace)
)
);
<ul id="marketplaces"></ul>