【发布时间】:2018-02-17 04:02:17
【问题描述】:
我在home.blade.php 中有这个 ajax 请求:
function send(event) {
event.preventDefault();
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')},
type: "POST",
url: "{{ route('add_action') }}",
data: {name: $("#name").val(), niceness: $("#niceness").val(), _token: "{{ Session::token() }}"}
});
}
在控制器中我有这个 postAction:
public function postInsertNiceAction(Request $request)
{
$this->validate($request, [
'name' => 'required|alpha|unique:nice_actions',
'niceness' => 'required|numeric',
]);
$action = new NiceAction();
$action->name = ucfirst(strtolower($request['name']));
$action->niceness = $request['niceness'];
$action->save();
$actions = NiceAction:: all();
if ($request->ajax()) {
return response()->json();
}
return redirect()->route('home');
}
我添加了这个元标记:
<meta name="_token" content="{{ csrf_token() }}">
所以当我运行它时,我得到了这个错误:
jquery-1.12.0.min.js:4 POST http://localhost:8000/do/add_action 422(无法处理的实体)
我尝试更改 jQuery 文件的版本,但它不起作用。
任何帮助将不胜感激!
我是 Laravel 的新手。
谢谢
【问题讨论】:
-
尝试从 ajax 帖子数据中删除
_tokendata:{name: $("#name").val(), niceness: $("#niceness").val()}你已经在标题中设置 -
还是同样的错误@user2486
-
无法处理的实体意味着您尝试发布的数据有问题,例如验证错误
-
这只是显示验证错误。您可以在 $this->validate() 之前 dd($request) 并检查它显示的内容吗?