尝试通过 AJAX 验证 Laravel Form 时出现错误 422

Ron*_*her 5 javascript php validation jquery laravel

我一直在尝试使用 Larave 的 Validator 类。正常提交表单时我可以让它工作,但是当我通过 AJAX 提交时出现错误:

\n\n
POST http://localhost/dashboard 422 (Unprocessable Entity)\n
Run Code Online (Sandbox Code Playgroud)\n\n

即使当我尝试我的表单的简单版本时:

\n\n
<div class="container">\n    <div class="row justify-content-center">\n        <div class="col-6">\n            @if ($errors->any())\n                @foreach($errors->all() as $error)\n                    <div>\n                        {{ $error }}\n                    </div>\n                @endforeach\n            @endif\n            <form action="/dashboard" method="post" id="test-form"> \n                @csrf\n                <input type="text" name="name" id="name" />\n                <input type="submit">\n            </form>\n        </div>\n    </div>\n</div>\n\n<script>\n    $(\'#test-form\').on(\'submit\', function(e)\n    {\n        e.preventDefault();\n        $.post($(this).attr(\'action\'), $(this).serialize())\n        .fail(function(data)\n        {\n            console.log(data);\n        })\n        .done(function(res)\n        {\n            alert(\'done\');\n        });\n    });\n</script>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的控制器:

\n\n
<?php\nnamespace App\\Http\\Controllers;\nuse Illuminate\\Http\\Request;\nclass DashboardController extends Controller\n{\n    public function index()\n    {\n        return view(\'dashboard\');\n    }\n\n    public function test(Request $request)\n    {\n        $validateData = $request->validate([\'name\' => \'required\']);\n\n        echo \'submitted\';\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

目标是提交表单,但现在我只想通过 AJAX 请求返回错误。我正在使用 Laravel 5.6。

\n\n

当我收到错误时,Console.log 会打印出来,并且状态测试显示为无法处理的实体。

\n\n

编辑:这是我的控制台日志中的数据:

\n\n
{readyState: 4, getResponseHeader: \xc6\x92, getAllResponseHeaders: \xc6\x92, setRequestHeader: \xc6\x92, overrideMimeType: \xc6\x92, \xe2\x80\xa6}\nabort\n:\n\xc6\x92 ( statusText )\nalways\n:\n\xc6\x92 ()\ncatch\n:\n\xc6\x92 ( fn )\ndone\n:\n\xc6\x92 ()\nfail\n:\n\xc6\x92 ()\ngetAllResponseHeaders\n:\n\xc6\x92 ()\ngetResponseHeader\n:\n\xc6\x92 ( key )\noverrideMimeType\n:\n\xc6\x92 ( type )\npipe\n:\n\xc6\x92 ( /* fnDone, fnFail, fnProgress */ )\nprogress\n:\n\xc6\x92 ()\npromise\n:\n\xc6\x92 ( obj )\nreadyState\n:\n4\nresponseJSON\n:\nerrors\n:\n{name: Array(1)}\nmessage\n:\n"The given data was invalid."\n__proto__\n:\nconstructor\n:\n\xc6\x92 Object()\nhasOwnProperty\n:\n\xc6\x92 hasOwnProperty()\nisPrototypeOf\n:\n\xc6\x92 isPrototypeOf()\npropertyIsEnumerable\n:\n\xc6\x92 propertyIsEnumerable()\ntoLocaleString\n:\n\xc6\x92 toLocaleString()\ntoString\n:\n\xc6\x92 toString()\nvalueOf\n:\n\xc6\x92 valueOf()\n__defineGetter__\n:\n\xc6\x92 __defineGetter__()\n__defineSetter__\n:\n\xc6\x92 __defineSetter__()\n__lookupGetter__\n:\n\xc6\x92 __lookupGetter__()\n__lookupSetter__\n:\n\xc6\x92 __lookupSetter__()\nget __proto__\n:\n\xc6\x92 __proto__()\nset __proto__\n:\n\xc6\x92 __proto__()\nresponseText\n:\n"{"message":"The given data was invalid.","errors":{"name":["The name field is required."]}}"\nsetRequestHeader\n:\n\xc6\x92 ( name, value )\nstate\n:\n\xc6\x92 ()\nstatus\n:\n422\nstatusCode\n:\n\xc6\x92 ( map )\nstatusText\n:\n"Unprocessable Entity"\nthen\n:\n\xc6\x92 ( onFulfilled, onRejected, onProgress )\n__proto__\n:\nObject\n
Run Code Online (Sandbox Code Playgroud)\n

Dwi*_*ght 6

当发出 Ajax 请求并且存在验证错误时,Laravel 将返回 422 响应。检查浏览器控制台中的响应 - 它应该显示包含请求验证错误的 JSON。您要发布到 Laravel 的 jQuery 代码很可能没有按照您期望的方式发送表单属性。