简体   繁体   English

由axios向laravel发布请求,并给出500错误

[英]post request by axios to laravel giving 500 error

I am using Laravel with Vue making http request with the axios. 我正在将Laravel与Vue一起使用axios发出http请求。 It is giving 500 error. 它给出了500错误。 My request code is 我的请求代码是

axios.post('user', {
       first_name: this.user.first_name,
       last_name: this.user.last_name,
       email: this.user.email,
       phone_number: this.user.phone_number,
       is_active: this.user.is_active,
       entity: this.user.entity,
       role_id: this.user.role_id
     })

Lavael Route is 熔岩路线现为

Route::resource('user', 'UserController');

And Controller is 控制器是

public function store(Request $request)
{
    $this->validate($request, [
      'first_name' => 'required|max:255',
      'last_name' => 'required|max:255',
      'email' => 'required',
      'is_active' => 'required'
    ]);

    $user = User::create([
      'first_name' => request('first_name'),
      'last_name' => request('last_name'),
      'email' => request('email'),
      'is_active' => request('is_active'),
      'phone_number' => request('phone_number'),
      'role_id' => request('role_id')
    ]);

    return response()->json([
      'user' => $user,
      'message' => 'Success'
    ], 200);

}

Your store method is correct. 您的商店方法是正确的。 You just need to specify the correct path to your axios post request, which means your component method should be like this : 您只需要为axios发布请求指定正确的路径,这意味着您的组件方法应如下所示:

axios.post('/user/store', {
       first_name: this.user.first_name,
       last_name: this.user.last_name,
       email: this.user.email,
       phone_number: this.user.phone_number,
       is_active: this.user.is_active,
       entity: this.user.entity,
       role_id: this.user.role_id
     }).then((res) => {
          // here clear your form and fetch your users list if you want 
     })
       .catch(error => {
          // here catch error messages from laravel validator and show them 
     });

and your store method should return validator messages error : 并且您的store方法应该返回验证器消息错误:

public function store(Request $request)
{   
    $validator = Validator::make($request->all(), [
             'first_name' => 'required|max:255',
             'last_name' => 'required|max:255',
             'email' => 'required',
             'is_active' => 'required'
    ]);
    if ($validator->fails()) {
        return response()->json(['errors'=>$validator->errors()],422);
    }

    $user = User::create([
      'first_name' => $request('first_name'),
      'last_name' => $request('last_name'),
      'email' => $request('email'),
      'is_active' => $request('is_active'),
      'phone_number' => $request('phone_number'),
      'role_id' => $request('role_id')
    ]);

    return response()->json([
      'user' => $user,
      'message' => 'Success'
    ], 200);
}

Try to change the following code block: 尝试更改以下代码块:

$user = User::create([
  'first_name' => request('first_name'),
  'last_name' => request('last_name'),
  'email' => request('email'),
  'is_active' => request('is_active'),
  'phone_number' => request('phone_number'),
  'role_id' => request('role_id')
]);

Replace with below: 替换为以下内容:

$user = User::create([
  'first_name' => $request->input('first_name'),
  'last_name' => $request->input('last_name'),
  'email' => $request->input('email'),
  'is_active' => $request->input('is_active'),
  'phone_number' => $request->input('phone_number'),
  'role_id' => $request->input('role_id')
]);
axios.post('/user/store', {
    first_name: this.user.first_name,
    last_name: this.user.last_name,
    email: this.user.email,
    phone_number: this.user.phone_number,
    is_active: this.user.is_active,
    entity: this.user.entity,
    role_id: this.user.role_id
  })
  .then(response => { 
    console.log(response)
 })
 .catch(error => {
   console.log(error.response)
 });

Try this code instead & always console.log your error with error.response to get all info & objects with full error details. 请尝试使用此代码,并始终使用error.response控制台记录您的错误,以获取具有完整错误详细信息的所有信息和对象。 Check in chrome console that all your POST datas from form are passed correctly to your database. 在chrome控制台中检查是否已将所有来自表单的POST数据正确传递到数据库。 In my case i figured out that my phone field where not passed due to typo mistake. 以我为例,我发现我的电话字段由于拼写错误而没有通过。 My console error Image file 我的控制台错误图像文件

or you can try 2nd method below for laravel users :- 或者您可以为laravel用户尝试以下第二种方法:-

  1. composer require barryvdh/laravel-cors
  2. Add the Cors\\ServiceProvider to your config/app.php provider's array Barryvdh\\Cors\\ServiceProvider::class, 将Cors \\ ServiceProvider添加到config / app.php提供程序的数组Barryvdh\\Cors\\ServiceProvider::class,
  3. To allow CORS for all your routes, add the HandleCors middleware in the $middleware property of app/Http/Kernel.php protected $middleware = [ \\Barryvdh\\Cors\\HandleCors::class, ]; 要允许所有路线使用CORS,请在app / Http / Kernel.php的$ middleware属性中添加HandleCors中间件。protected protected $middleware = [ \\Barryvdh\\Cors\\HandleCors::class, ];
  4. php artisan vendor:publish --provider="Barryvdh\\Cors\\ServiceProvider" php artisan vendor:publish --provider="Barryvdh\\Cors\\ServiceProvider" php工匠供应商:发布--provider =“ Barryvdh \\ Cors \\ ServiceProvider” php artisan vendor:publish --provider="Barryvdh\\Cors\\ServiceProvider"

Hope this helps you :) 希望这对您有所帮助:)

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

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