我不明白如何使用 axios 从 url 数组中获取数据。但我可以用fetch
. 以下代码完美运行:
const url = 'https://vimeo.com/api/oembed.json?url='
async index(videoUrls = []) {
try {
const response = await Promise.all(
// videoUrls.map(videoUrl => axios.$get(`${url}${encodeURIComponent(videoUrl)}`))
videoUrls.map(videoUrl => fetch(`${url}${encodeURIComponent(videoUrl)}`))
)
const results = await Promise.all(response.map(r => r.json()));
return results;
} catch (e) {
console.error(e)
}
}
当我拨打类似的电话时index(["https://vimeo.com/216850224", "https://vimeo.com/642263700"])
,我的控制台会显示一个数组,其中包含 vimeo 必须给我的所有视频元详细信息。太棒了。
但是当我注释掉使用fetch
和使用的那一行时axios
,我得到了一个CORS
错误。
从axios中的一堆url中获取数据的惯用方法是什么?
编辑
我也试过这个,但这个.all()
功能似乎不存在
async index(videoUrls = []) {
try {
const response = await axios.all(videoUrls.map(videoUrl => `${url}${encodeURIComponent(videoUrl)}`));
return response;
} catch (e) {
console.error(e)
}
}