简体   繁体   English

Laravel + VueJS + Axios 获取请求不起作用

[英]Laravel + VueJS + Axios Get Request Not Working

I am new to Vue and Laravel.我是 Vue 和 Laravel 的新手。 I want get users from the database with Axios.我想使用 Axios 从数据库中获取用户。 But it doesn't work.但它不起作用。 Axios post request works fine but get request not work. Axios 发布请求工作正常,但获取请求不起作用。 Please help me.请帮我。

Thanks.谢谢。 * *

UserController.php用户控制器.php

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

    }

Api.php api.php

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

Users.vue用户.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网络1

Network2网络2

I had the same problem today, I solved this problem using the new routing system that laravel 8 offers.我今天遇到了同样的问题,我使用 laravel 8 提供的新路由系统解决了这个问题。 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.我的问题在 web.php 文件中的一行。

Old-Web.php旧Web.php

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

New-Web.php新网.php

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

I changed this line and fixed my problem.我改变了这条线并解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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