0

I'm making an app with React Native and I'm struggling with a server problem. I made some HTTP requests without any body and it's all working, but now I'm trying to make a POST and it's working like a charm in Postman (request runner tool) but not when it's in the code:

enter image description here

So in my PHP I'm returning POST data that I sent in the body, and it's working here, but in my code it's returning null and my SQL is not executed:

save()
{
    this.updateDatabase(this.state.data).then((response) => {
        console.warn("id: " + response.id + "  status: " + response.status);
    });
}

async updateDatabase(newData)
{
    const response = await fetch('http://XXX.XXX.XXX:8888/updateApplication.php', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        mode: 'no-cors',
        body: JSON.stringify({
            data: newData,
        })
    });
    const json = await response.json();
    return json;
}

And here is my PHP:

$json = $_POST['data'];
    $data = json_decode($json)->{'data'};

    $appRequest = "UPDATE app_forms SET status = '".$data->{'status'}."'WHERE id = '".$data->{'id'}."'";

    $appResults = $connection->query($appRequest);

    $response = array ('id'=>$data->{'id'},'status'=>$data->{'status'});
    echo json_encode($response);

I don't understand why it won't work in my code... Thanks in advance for your help

Koko
  • 89
  • 9

1 Answers1

0

I think you have wrong format in body. Try this:

var data = new FormData();

data.append( "json", JSON.stringify( {data: newData } );

Then update request:

mode: 'no-cors',
body: data
tre
  • 845
  • 1
  • 8
  • 11
  • Thanks for your answer, I'm getting a "multipart != application/json" error and I tried to replace content type with "multipart/form-data" as I saw on internet but it's still null – Koko Sep 08 '17 at 18:25
  • Oh. Sorry, I did not pay attention. I think answer is here: https://stackoverflow.com/a/18867369/4957195 Getting data from post JSON. Postman sends form data, but your fetch script sends raw json – tre Sep 08 '17 at 18:29
  • I can't get something displayed when using the 'php://input'... It's just blank – Koko Sep 08 '17 at 18:46