开发者问题收集

如何从 API 访问数据?

2019-08-13
985

我正在设置一个小脚本,供我个人使用,通过测试 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 Uncaught TypeError: Cannot read property 'marketplace' of undefined
at Object.success (script.js:36)
at fire (jquery-1.9.1.js:1037)
at Object.fireWith [as resolveWith] (jquery-1.9.1.js:1148)
at done (jquery-1.9.1.js:8074)
at callback (jquery-1.9.1.js:8598)
at Object.send (jquery-1.9.1.js:8604)
at Function.ajax (jquery-1.9.1.js:7978)
at getSnkrs (script.js:26)
at HTMLDocument. (script.js:3)
at fire (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",
2个回答

我看到 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>');
}
Fajar AM
2019-08-13

这是一个没有 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>
Mohrn
2019-08-13