i am writing a simple user register using Laravel with a simple code
public function register()
{
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required'
]);
$user = User::create(request(['name', 'email', 'password']));
auth()->login($user);
return [
'status' => true,
'user' => $user
];
}
then i am sending the data form Next.Js :
using axios:
let config = {
method: 'post',
url: `http://localhost:8000/api/register`,
headers: {
'Content-Type': 'application/json',
},
data: values,
}
axios(config)
.then(json => console.log(json))
it's work fine, but i send an used email address, it will through 422 code, and axios can't catch the result
using fetch:
fetch('http://localhost:8000/api/register', {
method: "post",
mode: 'no-cors',
body: new URLSearchParams(data)
}).then(res => res.json())
.then(json => console.log(json))
also work but when use used email, it will send 302 code , and call the index /
I think that you have missed adding the .catch(...) to return the error object like the example in docs
Axios docs