I've encountered an issue with Axios while attempting to make a post request with a parameters object to a Laravel route.
If I use query parameters like ?username=user
, the post request works successfully. However, when I use an object, it fails:
Below is the Axios post request code snippet:
let params =
{
'username': 'help',
'password': 'me'
};
axios.post(
'https://cors.now.sh/https://jumprope.design/test-login', params, {
headers: {
'accept': 'application/json',
'accept-language': 'en_US',
'content-type': 'application/x-www-form-urlencoded',
'_token': 'UawpqT74cr9sr0Uut0lttZPE2YKDb1ckvXYpzNJW'
}
}).then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
Removing the params object and appending ?username=user
to the URL route makes it work, returning the data as expected from the request.
Now let's take a look at the Laravel (5.1) Controller:
protected function postTestRequest(Request $request)
{
$data = $request::all();
return response()->json($data);
}
The route functions well in both browser and Postman testing environments: https://cors.now.sh/https://jumprope.design/test-login
You can also find the code on CodePen here: CodePen
Any help or guidance would be greatly appreciated. Thank you!
Despite following the suggestions provided here, I still faced the same issues.