How to Resolve Error When Using axios to POST in Laravel and Vue Environment
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
⇨ Click here for the table of contents for Laravel articles
I summarized the solution when an error occurs when using axios to POST in a Laravel and Vue environment.
Laravel Framework 8.83.8
Vue 2.6
Verify the Situation Carefully
In general, when an error occurs in JavaScript frameworks, it is recommended to check the browser's console to see the details of the error.
Right-click⇨Inspect⇨Console
Or,
Command + Option + I keys
can be used to open the console.
It would be a good idea to check here first when an error occurs (even if no error occurs, it is worth checking).
In Case of 404 Error
If you encounter a 404 error, it means that the URL you are trying to POST to using axios is incorrect.
Make sure to POST to the routing created in Laravel.
To do so, carefully verify the URL output in the console to see if it matches the expected URL.
Here are some hints for resolution.
Routing in api.php includes a prefix
In Laravel routing, you can specify routes in web.php and api.php. If you write the routing in api.php like this,
Route::post('post/store',~~~);
it actually becomes
/api/post/store
This is because the file is configured to have the prefix "api" at the beginning.
Therefore, make sure to start with "api" like this:
axios.post('/api/post/store', obj).then()~~
Check the URL of the routing with:
php artisan route:list
Different Base URL in axios
For example, if you write:
axios.post("/post/store")
but when you check the console, it shows:
http://127.0.0.1:8000/post/post/store
This is because the current URL calling axios is "http://127.0.0.1:8000/post", causing the URL to be specified relative to it.
This can be resolved by setting the base URL for axios.
/resources/js/bootstrap.js
window.axios = require("axios")
// Specify absolute path when making axios requests
window.axios.defaults.baseURL = process.env.MIX_API_BASE_URL
// CSRF configuration (not directly related to the 404 error, but recommended)
window.axios.defaults.headers.common = {
"X-Requested-With": "XMLHttpRequest",
"X-CSRF-TOKEN": document
.querySelector('meta[name="csrf-token"]')
.getAttribute("content"),
}
.env
MIX_API_BASE_URL=http://127.0.0.1:8000
# Specify the base URL using env
If you made changes in the env file, restart with:
npm run watch
In Case of 405 Error
You might be mixing up GET and POST.
Check the Laravel routing and axios requests for any mistakes:
Route::get(~~~) //⇦ This GET and POST might be incorrect
axios.get(~~~~) //⇦ This GET and POST might be incorrect
In Case of 419 Error
Most of the time, a 419 error indicates an issue with CSRF configuration.
Check if there is a meta tag like this within the head tag
<head>
// Other content
<meta name="csrf-token" content="{{ csrf_token() }}">
</head>
Verify if the settings are written in resources/js/bootstrap.js
window.axios = require("axios")
// CSRF configuration
window.axios.defaults.headers.common = {
"X-Requested-With": "XMLHttpRequest",
"X-CSRF-TOKEN": document
.querySelector('meta[name="csrf-token"]')
.getAttribute("content"),
}
This should prevent a 419 error when POSTing.
In Case of 500 Error
A 500 error indicates an error in the server, specifically in the Laravel code.
Always check the logs for server-side errors.
storage/logs/laravel.log
This file contains the logs, with the latest logs typically at the bottom. It's best to check the latest log entries.
Check the logs starting from the most recent time
The most critical information is usually written at the top of the logs.
While the top entry in the log file shows older errors from the past, checking from the top of the most recent logs is recommended to find the most relevant information.
[previous exception] [object] (PDOException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project.users' doesn't exist at /project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:67)
[stacktrace]
#0 /project/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(67): PDO->prepare('insert into `us...', Array)
#1 project/vendor/laravel/framework/src/Illuminate/Database/Connection.php(457): Doctrine\\DBAL\\Driver\\PDOConnection->prepare('insert into `us...')
#2....
#3...
The structure of the logs is such that valuable information is written towards the top.
Summary
This concludes the article.
I hope it can be helpful to someone.
If you have any feedback or complaints, please contact me via Twitter DM.
That's all!
Popular Articles
Deploying PHP7.4 + Laravel6 Project to AWS EC2
Implementing Breadcrumbs in Laravel Using laravel-breadcrumbs