[Vuejs]-Export Excel with Laravel, VueJS and Inertiajs

6👍

Creator of Inertia.js here.

So, we recommend not manually sending the csrf token on each request like this.

A better approach is to use the CSRF functionality already built into axios for this. Axios is the HTTP library that Inertia uses under the hood.

Axios automatically checks for the existence of an XSRF-TOKEN cookie. If it’s present, it will then include the token in an X-XSRF-TOKEN header for any requests it makes.

The easiest way to implement this is using server-side middleware. Simply include the XSRF-TOKEN cookie on each response, and then verify the token using the X-XSRF-TOKEN header sent in the requests from axios.

Some frameworks, such as Laravel, do this automatically, meaning there is no configuration required. So, I’d recommend removing the csrf-token meta tag from your template, and removing the _token from your requests. That should take care of your issues.

That all said, keep in mind that you will not be able to download an Excel file from an Inertia request. All Inertia requests MUST return a valid Inertia response. You can use window.open for this. Something like this:

window.open(`/url/to/excel/download?slug=${generalDetails.}`, '_blank')

Leave a comment