简体   繁体   中英

Laravel + VueJS + Axios Get Request Not Working

I am new to Vue and Laravel. I want get users from the database with Axios. But it doesn't work. Axios post request works fine but get request not work. Please help me.

Thanks. *

UserController.php

    public function index()
    {
        
        return User::latest()->paginate(10);

    }

Api.php

Route::apiResources(['user' => 'App\Http\Controllers\API\UserController']);

Users.vue

<script>
import axios from 'axios'
    export default {
      data(){
        return{
          users : {},
          form: new Form({
            name: '',
            email:'',
            password: '',
            type: '',
            bio: '',
            photo: '',
          })
        }
      },
      methods:{
        loadUsers(){
          console.log('Load user');
          axios.get("api/user").then(({data})  => (this.users = data.data));
        },
      
        createUser(){
          this.form.post('api/user')
        }
      },
        created() {
            this.loadUsers();
        }

    }
</script>

Route List

Function successfully call but it didn't work.

Vue Debug

Network1

Network2

I had the same problem today, I solved this problem using the new routing system that laravel 8 offers. I put the resource Route like this:

Route::group(
  [
      'middleware' => 'api',
      'namespace'  => 'App\Http\Controllers',
  ],
  function ($router) {
      Route::resource('user', 'API\UserController');
  }
);

Thank you for your help. I found my problem. My problem one line in the web.php file.

Old-Web.php

    Route::get('{path}', [App\Http\Controllers\HomeController::class, 'index'])->where('path','.*');

New-Web.php

    Route::get('{path}', [App\Http\Controllers\HomeController::class, 'index'])->where('/path','([A-z\d-\/_.]+)?');

I changed this line and fixed my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM