Javascript 并行使用axios进行post调用

Javascript 并行使用axios进行post调用,javascript,node.js,promise,async-await,axios,Javascript,Node.js,Promise,Async Await,Axios,我有一个正在运行的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.str

我有一个正在运行的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);
    });
如果有更好的办法,请告诉我


顺便说一句,如果我运行这两个分开的post请求,一切正常

您需要向axios提供承诺。所有不是它们的配置对象

axios.all([
axios({
方法:“post”,
网址:'https://oauth2.-arch.mand.com/oauth2/token',
数据:qs.stringify({
“授权类型”:“客户端凭据”
}, {
“范围”:“运行:crud”
}),
标题:{
“接受”:“应用程序/json”,
“授权”:“基本NTgwNjgtMDhhZTczOGNl”,
“内容类型”:“应用程序/x-www-form-urlencoded”,
}
}),
axios({
方法:“post”,
网址:'https://oauth2.-arch.mand.com/oauth2/token',
数据:qs.stringify({
“授权类型”:“客户端凭据”
}, {
'scope':'exe:crud'
}),
标题:{
“接受”:“应用程序/json”,
“授权”:“基本NTgwNjgtMDhhZTczOGNl”,
“内容类型”:“应用程序/x-www-form-urlencoded”,
}
})
]).然后(axios.spread((…响应:AxiosResponse[])=>{
const aToken=响应[0];
const bToken=响应[1];
}).catch(函数(错误:任意){
日志(“错误:+错误”);
});

请记住,
axios.all
axios.spread
都是


直接使用

您需要向axios提供承诺。所有不是它们的配置对象

axios.all([
axios({
方法:“post”,
网址:'https://oauth2.-arch.mand.com/oauth2/token',
数据:qs.stringify({
“授权类型”:“客户端凭据”
}, {
“范围”:“运行:crud”
}),
标题:{
“接受”:“应用程序/json”,
“授权”:“基本NTgwNjgtMDhhZTczOGNl”,
“内容类型”:“应用程序/x-www-form-urlencoded”,
}
}),
axios({
方法:“post”,
网址:'https://oauth2.-arch.mand.com/oauth2/token',
数据:qs.stringify({
“授权类型”:“客户端凭据”
}, {
'scope':'exe:crud'
}),
标题:{
“接受”:“应用程序/json”,
“授权”:“基本NTgwNjgtMDhhZTczOGNl”,
“内容类型”:“应用程序/x-www-form-urlencoded”,
}
})
]).然后(axios.spread((…响应:AxiosResponse[])=>{
const aToken=响应[0];
const bToken=响应[1];
}).catch(函数(错误:任意){
日志(“错误:+错误”);
});

请记住,
axios.all
axios.spread
都是


直接使用

如果稍微更改语法怎么办?
。然后(axios.spread((aToken,bToken)=>{//req.console.log的输出('aToken',aToken',bToken'))
@CristianGabor-谢谢我试过了,我得到了相同的结果,这是我放在
axios中的两个请求对象。所有
。我可以在调试url中看到方法等,我想得到令牌…有什么想法吗?@CristianGabor-谢谢我试过了,我得到了相同的结果,这是我放在我的两个请求对象在
axios.all
。我可以在调试url中看到方法等,我想得到令牌…有什么想法吗?如果您稍微更改一下语法会怎么样?
。然后(axios.spread((aToken,bToken)=>{//req.console.log的输出('aToken',aToken,'bToken',bToken)})
@CristianGabor-谢谢我试过了,我得到了相同的结果,这是我放在
axios中的两个请求对象。所有
。我可以在调试url中看到方法等,我想得到令牌…有什么想法吗?@CristianGabor-谢谢我试过了,我得到了相同的结果,这是我放在我的两个请求对象在
axios.all
。我可以在调试url中看到方法等,我想获得令牌…有什么想法吗?非常感谢,如果你能提供一个示例,说明我应该如何在没有弃用的情况下更改代码,那就太好了。非常感谢,如果你能提供一个示例,说明我应该如何在没有弃用的情况下更改代码,那就太好了。t汉克斯