As I read in the documentation, when using ajax, the answer is always returned in json, but I can not get the information to print.
The message returned in the console is:
http://localhost/projeto/cadastra/usuario 422 (Unprocessable Entity)
and the details
{message: "The given data was invalid.", errors: {email: ["validation.unique"]}}
errors:{email: ["validation.unique"]}
message:"The given data was invalid."
and my ajax code is:
$('#cad_usu').on('submit',function(e){
e.preventDefault();
$.ajax({
url: "{{ url('cadastra/usuario') }}",
data: $(this).serializeArray(), //$(this).serialize() tentei tbm
type: 'post',
dataType: 'json',
success: function(data){
console.log(data.message);
$('#retorno').html(data.message); //data.responseJSON tentei tbm
}
});
});
missing controller
public function cadastra(Request $request)
{
$request->validate([
'nome' => 'required|max:55',
'email' => 'required|unique:usuarios',
'senha' => 'required',
]);
$c = new Usuario();
$c->nome = $request->nome;
$c->email = $request->email;
$c->senha = Hash::make($request->senha);
$c->save();
}