这是我第一次在 Laravel 中使用 JQuery 和 Ajax 验证。我正在尝试使用 Laravel 请求规则验证表单。 似乎服务器验证了字段,因为当我没有填写请求时,它会向我发回错误,但是当我这样做时,我在控制台上收到此错误 SyntaxError: JSON Parse error: Unrecognized token '<'
这是我写的代码:
技术员表格请求
public function rules()
{
return [
'nome' => 'required',
'cognome' => 'required',
'ruolo_principale' => '',
'antincendio' => '',
'primosoccorso' => '',
'rischioelettrico' => '',
'lavoroinquota' => '',
//
];
TechnicianController(存储方法)
public function store(TechnicianFormRequest $request)
{
$validated = $request->validated();
$technician = Tecinfo::create($validated);
return redirect()->action('TechnicianController@index')->with('success', 'Tecnico aggiunto con successo!');
// return dd($request->all());
}
Ajax 代码: (无法粘贴js代码,所以我添加了图片)
感谢所有帮助我的人
瓦莱里奥
最佳答案
您正在 ajax
中解析 Json
,但没有从 store
方法返回 json
根据 Laravel 文档:
The
json
method will automatically set theContent-Type
header toapplication/json
, as well as convert the given array to JSON using thejson_encode
PHP function
在 Controller 中使用以下内容返回json
return response()->json([
'success' => 'Tecnico aggiunto con successo!',
]);
关于javascript - Ajax验证和SyntaxError : JSON Parse error: Unrecognized token '<' on Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60798525/