【问题标题】:422 Unprocessable Entity Laravel 5.6 AJAX request Using Request rules returning response in json422 Unprocessable Entity Laravel 5.6 AJAX请求使用请求规则返回json中的响应
【发布时间】:2018-04-27 06:57:52
【问题描述】:

获取422 Unprocessable Entity 当我的请求中有重复的电子邮件或用户名时,但如果我使用唯一的电子邮件和用户名注册它工作正常。 laravel 请求规则的这个问题 我的规则文件看起来像。

namespace App\Http\Requests;

use App\Http\Requests\Request;

class RegisterRequest extends JsonRequest
{

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name' => 'required',
            'username' => 'required|unique:users,username',
            'email' => 'required|email|unique:users,email',
            'password' => 'required',
            'confirm_password' => 'required|same:password'
        ];
    }
}

我的 Handler.php 文件。

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Contracts\Validation\Validator;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{

    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];

    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
    $response = new JsonResponse(['data' => [], 
             'meta' => [
                'success' => false,
                'message' => 'The given data is invalid', 
                'errors' => $validator->errors()
             ]], 422);

   throw new HttpResponseException(response()->json($validator->errors(), 422)); 
}

protected function invalidJson($request, ValidationException $exception)
{
    return response()->json(['success' => false, 'errors' => $exception->errors()], $exception->status);
}
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }
}

我尝试为我在处理程序文件中全局添加此代码的所有请求返回 json。但我仍然收到442 Status code 可能是返回 json 的问题,如果有人可以提供帮助,请查看我的代码。

【问题讨论】:

  • 您的规则要求用户名和电子邮件都是唯一的。我不明白问题出在哪里。
  • 规则返回正确信息但状态为 422 Jquery 无法识别状态。

标签: php laravel validation exception


【解决方案1】:

通过添加状态代码 200 修复 是的,它很棘手,但它的工作原理

<?php

    namespace App\Exceptions;

    use Exception;
    use Illuminate\Validation\ValidationException;
    use Illuminate\Auth\Access\AuthorizationException;
    use Illuminate\Database\Eloquent\ModelNotFoundException;
    use Symfony\Component\HttpKernel\Exception\HttpException;
    use Illuminate\Contracts\Validation\Validator;

    use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

    class Handler extends ExceptionHandler
    {

        protected $dontReport = [
            AuthorizationException::class,
            HttpException::class,
            ModelNotFoundException::class,
            ValidationException::class,
        ];

        protected $dontFlash = [
            'password',
            'password_confirmation',
        ];
        public function report(Exception $exception)
        {
            parent::report($exception);
        }

        protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
    {
        $response = new JsonResponse(['data' => [], 
                 'meta' => [
                    'success' => false,
                    'message' => 'The given data is invalid', 
                    'errors' => $validator->errors()
                 ]], 200);

       throw new HttpResponseException(response()->json($validator->errors(), 200)); 
    }

    protected function invalidJson($request, ValidationException $exception)
    {
        return response()->json(['success' => false, 'errors' => $exception->errors()], $exception->status);
    }
        public function render($request, Exception $exception)
        {
            return parent::render($request, $exception);
        }
    }

【讨论】:

    猜你喜欢
    • 2023-01-07
    • 2013-02-08
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    相关资源
    最近更新 更多