doucang6914 2018-11-06 13:20
浏览 514

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条回答

  • duanlu1950 2018-11-06 13:24
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型