本文介绍了带有未知数量的回调参数的Axios Spread()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用axios处理未知数量的AJAX请求(1个或更多),并且我不确定如何处理响应.我想要一些类似的东西:
I need to process an unknown number of AJAX requests (1 or more) with axios, and I am not sure how to handle the response. I want something along the lines of:
let urlArray = [] // unknown # of urls (1 or more) axios.all(urlArray) .then(axios.spread(function () { let temp = []; for (let i = 0; i < arguments[i].length; i++) temp.push(arguments[i].data); }));其中参数将包含axios发送的回调响应.问题在于arguments包含给定的字符串url,而不是实际的响应.我该如何解决这个问题?
where arguments will contain the callback responses sent by axios. The problem is that arguments contains the given string urls instead of the actual responses. How can I resolve this problem?
推荐答案您需要在某个地方提出实际的请求.然后不要使用spread而是仅使用then来接收结果数组:
You somewhere will need to make the actual requests. And then don't use spread but only then to receive the array of results:
let urlArray = [] // unknown # of urls (1 or more) let promiseArray = urlArray.map(url => axios.get(url)); // or whatever axios.all(promiseArray) .then(function(results) { let temp = results.map(r => r.data); … });更多推荐
带有未知数量的回调参数的Axios Spread()
发布评论