首页 > 解决方案 > 如何在亚马逊上获取商品的价格?

问题描述

我需要列出物品清单和价格。我从亚马逊得到这些物品。我在谷歌上搜索了这个,我只发现了如何使用 Node.js 获取价格。如何使用 JavaScript 获取这些商品的价格?

let itemPrice;

function getPrice(url) {
    let price;
    price = // gets the item's price
    return price;
}

window.onload = function () {
    itemPrice = getPrice(url);
}

标签: javascripthtml

解决方案


getPrice功能上,需要使用ajax请求从 url 获取项目价格,因此它将是asyncfunc 部分。

因此,最好让getPrice函数异步返回Promise进程。

let itemPrice;

function getPrice(url) {
  return new Promise((resolve, reject) => {
    let price;
    price = 35; // gets the item's price
    return resolve(price);
  });
}

window.onload = function () {
  getPrice("testurl")
    .then((price) => {
      itemPrice = price;
      console.log('Item Price', itemPrice);
    })
    .catch((error) => {
      // Handling error
    });
}


推荐阅读