我正面临一个奇怪的问题,关于axios的get调用。
try {
console.log('assetAddress', assetAddress);
var options = {
method: 'GET',
url: `https://testnets-api.opensea.io/api/v1/events?only_opensea=false&limit=20&asset_contract_address=${assetAddress}`,
headers: { "Accept": 'application/json' }
};
console.log(options.url)
res = await axios.request(options);
return res.data;
} catch (e) {
//console.log(e)
}当我在url中传递assetAddress,然后尝试获得结果时,它显示的是未定义的结果。但是当我用实际的0x15352F80426ec9b94412b45242d7040b5dFeB5E6硬编码assetAddress时。
为什么会有线索?
发布于 2022-06-07 08:15:45
这里的问题似乎是assetAddress变量的作用域。
下面是使用两个不同版本的两个示例
您可以在这里使用.then和.catch函数而不是使用async / await。
const assetAddress = "0x15352F80426ec9b94412b45242d7040b5dFeB5E6"
var options = {
method: 'GET',
url: `https://testnets-api.opensea.io/api/v1/events?only_opensea=false&limit=20&asset_contract_address=${assetAddress}`,
headers: {
"Accept": 'application/json'
}
};
axios.request(options)
.then(res => console.log(res.data))
.catch(error => console.log(error))<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.0.0-alpha.1/axios.min.js"></script>
如果您还想使用经典版本,请看下面的版本。
const assetAddress = "0x15352F80426ec9b94412b45242d7040b5dFeB5E6"
async function loadData() {
console.log("Loading Data")
var options = {
method: 'GET',
url: `https://testnets-api.opensea.io/api/v1/events?only_opensea=false&limit=20&asset_contract_address=${assetAddress}`,
headers: {
"Accept": 'application/json'
}
};
try {
const res = await axios.request(options)
console.log(res)
} catch (error) {
console.log(error)
}
}
document.getElementById('loadDataButton').addEventListener('click', loadData)<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.0.0-alpha.1/axios.min.js"></script>
<button id="loadDataButton">Load data</button>
https://stackoverflow.com/questions/72527880
复制相似问题