如何使用惯性发送多个 HTTP 请求

时间:2021-05-20 15:36:55

标签: axios inertiajs

Inertia 提供了一个非常酷的辅助方法,它基于 axios,我建议,例如:

Inertia.post('/users', {
  name: 'John Doe',
  email: 'john.doe@example.com',
})

我在 Inertia 文档中没有找到它,所以我在这里问 - 是否有可能像使用 axios 那样使用 Inertia 执行多个 HTTP 请求?

// execute simultaneous requests 
axios.all([
  axios.get('https://api.github.com/users/mapbox'),
  axios.get('https://api.github.com/users/phantomjs')
])
.then(responseArr => {
  //this will be executed only when all requests are complete
  console.log('Date created: ', responseArr[0].data.created_at);
  console.log('Date created: ', responseArr[1].data.created_at);
});

或者我应该只使用 axios 来做到这一点?

1 个答案:

答案 0 :(得分:0)

据我所知;不,您不能像使用 axios 那样使用 Inertia 执行多个 HTTP 请求。所以,我会使用 axios 来做到这一点。

我对文档的解释如下:Inertia works 通过拦截前端的点击,并通过 XHR 进行访问。因此,它旨在一次单击一次。 visit method 还表明它通过使用一个 url 调用 axios 来进行这次访问。

此外,由于您请求的是纯 JSON,因此 Inertia 的作者建议在处理纯 JSON 时直接使用 XHR,因为“一个 Inertia 请求必须收到一个 Inertia 响应”(source)。