首页 > 解决方案 > 如何使用惯性发送多个 HTTP 请求

问题描述

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

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

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

// 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 来做到这一点?

标签: axiosinertiajs

解决方案


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

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

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


推荐阅读