编程语言
首页 > 编程语言> > Javascript:axios:批处理请求

Javascript:axios:批处理请求

作者:互联网

如何使用axios进行批处理请求:

我有以下批量请求

POST /batch HTTP/1.1
Content-Type: multipart/mixed; boundary=====1340674896===

--====1340674896===

GET /contacts/479038 HTTP/1.1
Content-Type: application/json

--====1340674896===

GET /contacts/299430 HTTP/1.1
Content-Type: application/json

--====1340674896===--

如何使用axios获得响应:我正在尝试下面的内容

     const config = {
        headers: {
            'content-type': 'multipart/mixed; boundary=====1340674896==='
        }
     }
     axios.post('/batch',data,config)
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });

如何传递数据

    --====1340674896===

GET /contacts/479038 HTTP/1.1
Content-Type: application/json

--====1340674896===

GET /contacts/299430 HTTP/1.1
Content-Type: application/json

--====1340674896===--

解决方法:

使用axios执行一批请求:

// Performing multiple concurrent requests

function getUserAccount() 
{
  return axios.get('/user/12345');
}
 
function getUserPermissions() 
{
  return axios.get('/user/12345/permissions');
}
 
axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(function (acct, perms) 
  {
    // Both requests are now complete
  }));

您可以使用try / catch和async / await来获取使用axios的响应:

const config = 
{
  headers: {'content-type': 'multipart/mixed;' }
}
try
{
  const response = await axios.post('/batch',data,config);
  
  //response
}
catch (ex)
{
  console.error(ex);
}

标签:javascript,batch-processing,axios
来源: https://codeday.me/bug/20190710/1426348.html