Laravel&Vue:Axios GET呼叫返回200状态但没有响应

EDIT:

So I just got this working by changing the axios get call to a post call, same with the route, any idea why did this work???

from: javascript axios.get('/globals/get-logged-user')

and

php Route::get('/globals/get-logged-user','GlobalsController@getLoggedUser');

to:

javascript axios.post('/globals/get-logged-user')

and

php Route::post('/globals/get-logged-user','GlobalsController@getLoggedUser');

I'm trying to get current authenticated user (if exists) for my Header vue component, I'm also using vuex store modules to keep my code organized and decoupled.

The proccess goes like this:

When the header component is mounted() I dispatch the action getLoggedUser.

Now inside the Globals store I make axios get call to route /globals/get-logged-user

If the call is succesfull I update the state, now my Header component should be able to access thelogged user data with the help of mapState vuex helper.

Now what actually happens:

The axios get call is done, it returns a positive 200 status code but the response is blank, nothing gets sent back from the backend.

This is my network tab:

https://i.imgur.com/hlD9bAY.png

As you can see the other ajax requests return actual data but the get call returns nothing, I also noticed the type of the GET call is html, could it be this?

My component:

<template>
<header class="header_maincontainer">
    <div class="header_small_container">
         <span>{{ loggedUser.email }}</span>   
    </div>
</header>
</template>
<!--SCRIPTS-->
<script>
import { mapState, mapActions } from 'vuex'
export default {
name: 'MainHeader',
computed: {
    
    //...mapState('Globals', ['allGlobals', 'loggedUser', 'globals']),
    ...mapState('Globals', [ 'loggedUser']),
},

mounted() {
    console.log(this.$options.name+' component successfully mounted');
    this.getLoggedUser();
},

methods:{
    //component actions declaration
    ...mapActions('Globals', ['getLoggedUser']),
  
}
};
</script>
<!--STYLES-->
<style scoped>

</style>

My Globals vuex module:

import axios from 'axios'

//STATE
const state = {
    
    loggedUser:'',
    
    loadingStatus:0
}

//GETTERS
const getters = {




}

//ACTIONS
const actions = {

    

    
    getLoggedUser ({ commit }) {

        commit( 'SET_LOAD_STATUS', {status: 1} );
        axios.get('/globals/get-logged-user')
        .then((response) => {
            commit('GET_LOGGED_USER', { loggedUser: response.data.loggedUser })
            console.log(response.data.loggedUser);
            commit( 'SET_LOAD_STATUS', {status: 2} );
        },
        (error) => {
            console.log(error);
            commit( 'SET_LOAD_STATUS', {status: 3} );
        })
    },


    

}

//MUTATIONS
const mutations = {

    
    GET_LOGGED_USER (state, loggedUser) {
        state.loggedUser = loggedUser;
    },

  

}

export default {
  namespaced: true, state, getters, actions, mutations
}

And my laravel backend:

<?php

namespace App\Http\Controllers;

use App\Models\Globals;
use Illuminate\Http\Request;

class GlobalsController extends Controller
{
    
    


    public function getLoggedUser(){

        $loggedUser = Auth::user();
    
        return response()->json([
        
            'loggedUser' => $loggedUser

        ]);

    }


   
}

Please help me, I'm still stuck with this after all these days.

</div>

1个回答



如果您收到的200响应为空,则可能是错误的路线。</ p>

尝试添加调试输出或断点以检查您是否使用了正确的控制器功能。</ p>
</ div>

展开原文

原文

If you are receiving a 200 with an empty response you might be hitting the wrong route.

Try adding debug outputs or breakpoints to check that you are in the correct controller function.

douqie1852
douqie1852 尝试添加dd('test'); 在控制器功能开始时调用您的路线。 如果它不打印“test”,则表示您的控制器功能错误。 在这种情况下,您的路线可能配置错误。 有关dd辅助函数的更多信息:laravel.com/docs/5.7/helpers#method-dd
接近 2 年之前 回复
duanduan1993
duanduan1993 你能给我一些如何做到这一点的例子
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐