javascript - 如何在亚马逊上获取商品的价格?
问题描述
我需要列出物品清单和价格。我从亚马逊得到这些物品。我在谷歌上搜索了这个,我只发现了如何使用 Node.js 获取价格。如何使用 JavaScript 获取这些商品的价格?
let itemPrice;
function getPrice(url) {
let price;
price = // gets the item's price
return price;
}
window.onload = function () {
itemPrice = getPrice(url);
}
解决方案
在getPrice
功能上,需要使用ajax
请求从 url 获取项目价格,因此它将是async
func 部分。
因此,最好让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
});
}
推荐阅读
- linux - 我的目录名有问题,我不知道该怎么办
- flutter - FLUTTER:如何从 json api 的缓存中访问会话 id 和 csrf
- oauth-2.0 - 隐式流在 Azure AD 中没有正确反应
- php - 在 PHP 中切换对象的大小写
- django - Django CSRF Cookie 未为 Chrome、Safari 但不是 Firefox 设置
- javascript - 数据表未正确对齐分页元素
- spring-security - 使用 Spring + RSocket + Websocket 添加 CORS 配置
- python - 绘制 seaborn 箱线图
- r - 使用条件和循环替换列中的某些数值
- powershell - Get-FileHash 在整个 C: 盘