【发布时间】:2020-09-21 11:20:06
【问题描述】:
我有这个正在运行的 axios 请求 ok 我得到了 http 200
import {AxiosResponse} from "axios";
const axios = require('axios').default;
const qs = require('qs');
//First query
axios({
method: 'post',
url: 'https://oauth2.-arch.mand.com/oauth2/token',
data: qs.stringify({'grant_type': 'client_credentials'}, {'scope': 'run:crud'}),
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
'Content-Type': 'application/x-www-form-urlencoded',
}
}).then(function (response: AxiosResponse) {
console.log(response.data);
}).catch(function (error: any) {
console.log("Error: " + error);
});
//第二次查询(唯一不同的是范围)
axios({
method: 'post',
url: 'https://oauth2.-arch.mand.com/oauth2/token',
data: qs.stringify({'grant_type': 'client_credentials'}, {'scope': ‘exe:crud’}),
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
'Content-Type': 'application/x-www-form-urlencoded',
}
}).then(function (response: AxiosResponse) {
console.log(response.data);
}).catch(function (error: any) {
console.log("Error: " + error);
});
现在我想并行运行两个请求并得到response.data的结果(我发送到请求/标头等的数据)
我试过了,但我得到的响应就像请求一样,我没有得到令牌/数据?任何想法 如何同时使用这两个请求?
axios.all([{
method: 'post',
url: 'https://oauth2.-arch.mand.com/oauth2/token',
data: qs.stringify({'grant_type': 'client_credentials'}, {'scope': 'run:crud'}),
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
'Content-Type': 'application/x-www-form-urlencoded',
}
},
{
method: 'post',
url: 'https://oauth2.-arch.mand.com/oauth2/token',
data: qs.stringify({'grant_type': 'client_credentials'}, {'scope': 'exe:crud'}),
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
'Content-Type': 'application/x-www-form-urlencoded',
}
}]).then(axios.spread((...responses: AxiosResponse[]) =>{
const aToken = responses[0];
const bToken = responses[1];
}).catch(function (error: any) {
console.log("Error: " + error);
});
如果有更好的方法,请告诉我
顺便说一句,如果我将这两个帖子请求分开运行,一切正常
【问题讨论】:
-
如果你稍微改变一下语法会怎样?
.then(axios.spread((aToken, bToken) => { // output of req. console.log('aToken', aToken, 'bToken', bToken)}) -
@CristianGabor - 谢谢我已经尝试过了,我得到了相同的结果,这是我放入
axios.all的两个请求对象。我可以在调试 url 中看到方法等,我想获取令牌...有什么想法吗? -
@CristianGabor - 谢谢我已经尝试过了,我得到了相同的结果,这是我放入
axios.all的两个请求对象。我可以在调试 url 中看到方法等,我想获取令牌...有什么想法吗?
标签: javascript node.js promise async-await axios