0
votes

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";
    }
2
Try it with this return return response()->json([ 'state' => 'success' ]);Maraboc
no luck brother :(WahidSherief
Just give it a try, instead of body:JSON.stringify(yourObj), use this, body: 'data=' + JSON.stringify(yourObj) . And then check at the server side.Aniruddha Shevle

2 Answers

0
votes

Server return is not a json. You can post the return of the requisition?

0
votes

what the answer above is trying to say is that you are supposed to do

return response()->json("success");

instead of

return "success";

its here https://laravel.com/docs/5.5/responses#json-responses