【问题标题】:How to send multiple HTTP requests with inertia如何使用惯性发送多个 HTTP 请求
【发布时间】:2021-08-09 21:35:00
【问题描述】:

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

【问题讨论】:

    标签: axios inertiajs


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多