samedi 7 juillet 2018

can not save react native stringfy data using laravel controller

this is the react native code

    fetch('https://sssdff.000webhostapp.com/save/', {
        method: 'post',
        header:{
            'Accept': 'application/json',
            'Content-type': 'application/json'
        },
        body:JSON.stringify({
            name: "john",
            price: 145,

        })

    })
    .then((response) => response.json())
        .then((response) =>{
            alert("sucess");
        })
        .catch((error)=>{
            alert("error"+error);
            console.error(error);
        });

This is the laravel code

  public function save(Request $request)
{

    $data = $request->name;

    $coin = new Coin();
    $coin->name=$data;
    $coin->price= 125;
    $coin->save();


 return response()->json([ 'state' => 'success' ]);

}

when i pass a string value, like $coin->name="jack"; it successes and save data to database. But when i try to pass parameter value then got a error.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire