0

I'm having a really tough time fetching HTTP Post data sent from Vue, in my Laravel API.

What I have is extremely simple.

axios({
    method: 'post',
    url: 'http://localhost:8888/api/vue-data',
    data: { firstName: "Dally" },
    config: { headers: { 'Content-Type': 'multipart/form-data' }}
    })
    .then(response => {
        console.log(response.data)
    })
    .catch(e => {
        this.errors.push(e)
})

And then the function in my Laravel controller is this...

public function vueData(Request $request)
{
    return $request->firstName;
}

The data is being sent as I can see it in Chrome Dev tools but why can't I fetch it in Laravel?

I've tried vue-resource as opposed to Axios but still no luck.

UPDATE #1

So when I use Axios I get the following error.

enter image description here

And when I use vue-resource I get this.

enter image description here

Data is passing...

enter image description here

UPDATE #2

If I change the Laravel controller code to this, I get a response in VueJS. Seems like the controller function in Laravel isn't receiving the Vue data or that I'm accessing it wrong.

public function vueData(Request $request)
{
    return "Testing";
}

5
  • What about debugging on $request->all() then figure out if there's data keyed firstName there? Commented Aug 22, 2018 at 11:50
  • 1
    but why can't I fetch it in Laravel? Well, what's happening? Do you get an error? Commented Aug 22, 2018 at 11:50
  • You need to send FormData object since you are setting 'multipart/form-data' Commented Aug 22, 2018 at 11:52
  • Have updated my original post guys. Commented Aug 22, 2018 at 13:23
  • what does the 500 errors says? Commented Aug 22, 2018 at 15:44

4 Answers 4

0

If you POST'ing to Laravel you need to specify CSRF token in request.

Add to your <head>:

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

And set default X-CSRF-TOKEN for axios:

axios.defaults.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
Sign up to request clarification or add additional context in comments.

1 Comment

You only need to do that if your using web.php for routing mate. I'm using api.php so I don't think that's the issue.
0

If you POST to Laravel you need to specify CSRF token in request.

Add to your :

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

First you should add following line in app.js

axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf- 
token"]').getAttribute('content')
};

const config = { headers: { 'Content-Type': 'multipart/form-data' } };  
 axios.post('/admin/theme/',{name: "yourname"},config).then(response=>{
 if(response.data.success === true){

 }).catch((error) => {

 });

i hope it's help for you.

1 Comment

You only need to do that if your using web.php for routing mate. I'm using api.php so I don't think that's the issue.
0

As i can't see what error are you getting, my best guest is this

The errors is because you're trying to get the data like this return $request->firstName;, but in the image you shown you're sending

{
    params : {
        firstName : 'Dally'
    }
}

Try getting params first then returning what you want like

public function vueData(Request $request)
{
    $data = $request->input('params');
    return $data->firstName;
    //if this fails, maybe params contains a array so do
    //return $data['firstName'];
}

Comments

0

Solved the issue. Was such a noob mistake it's embarrassing. I was missing the following in my controller.

use Illuminate\Http\Request;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.