0
From what I read in the documentation, when using ajax, the answer is always returned in json, but I cannot receive the information to print.
The message returned on 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 the 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();
}
Already tried to access via
data.message
?– Woss
I tried yes, I also tried with responseJSON, which I saw in other tips, but it’s like it’s killing the application.
– Luiz