Axios response data is always set as empty string in laravel

1.8k views Asked by At

I am having some strange problem with axios I never had before, any returned response from laravel backend to vue frontend is empty..

return response()->json(['message' => 'Success!']);

This line is just after the post is edited...

I also tried:

return response(['message' => 'Success!']);
return ['message' => 'Success!'];

And when I console.log() response:

axios.post(url).then((response)=>{
    console.log(response);
});

I get everything about response and data as empty string, I am having this problem in Laravel 5.7 where in 5.6 works just fine...

1

There are 1 answers

0
jpm On

In you blade make sure you have csrf_token in the head

xyz.blade.php

<head>
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head

From Vue or React: Make sure your method is right get, post, delete, put in routes file (web.php or anything)

axios.post('/some/url', {
           post_param_a : 1, 
            post_param_b : 2
         }).then(response => {
              //your value should be in response.data object
              console.log(response.data.message)
            })
            .catch(function (error) {
                console.log(error);
            });

Inside Controller :

public function methodName(){
    return response()->json(['message' => 'Success!']);
}

Alternatively you can also debug Right click on browser >> Inspect >> Network tab >> Refer to screenshot.

enter image description here

You can also dd($response) your response and view in the inspector in response section