Ajax validation and SyntaxError: JSON Parse error: Unrecognized token '<' on Laravel

Ajax validation and SyntaxError: JSON Parse error: Unrecognized token '<' on Laravel

这是我第一次在 Laravel 中使用 JQuery 和 Ajax 验证。我正在尝试使用 Laravel 请求规则验证表单。 似乎服务器验证了字段,因为当我没有填写请求时它会向我发回错误但是当我这样做时我在控制台上收到此错误 SyntaxError: JSON 解析错误:无法识别的标记“<”

那是我写的代码:

TechnicianFormRequest

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 code image

感谢所有帮助我的人

瓦莱里奥

您正在 ajax 中解析 Json,而不是 returning json 来自您的 store 方法

根据 Laravel 文档:

The json method will automatically set the Content-Type header to application/json, as well as convert the given array to JSON using the json_encode PHP function

在您的控制器中使用下面的内容 return json

return response()->json([
    'success' => 'Tecnico aggiunto con successo!',
]);