I am trying to post data from react native app by json.stringify to Laravel controller. But getting "JSON Parse error: Unrecognized token '<'". I need help how to resolve it. My code are given below:
js code
fetch(API_URL+'/signup', {
method: 'post',
header:{
'Accept': 'application/json',
'Content-type': 'application/json'
},
body:JSON.stringify({
name: userName,
email: userEmail,
password: userPassword
})
})
.then((response) => response.json())
.then((response) =>{
alert(response);
})
.catch((error)=>{
console.error(error);
});
Laravel:
public function registerUser()
{
$this->validate(request(), [
'email' => 'required|email',
'name' => 'required',
'password' => 'required|min:4|confirmed'
]);
$user = User::create(request(['email', 'name', 'password']));
return "success";
}
return response()->json([ 'state' => 'success' ]);
– Marabocbody:JSON.stringify(yourObj)
, use this,body: 'data=' + JSON.stringify(yourObj)
. And then check at the server side. – Aniruddha Shevle