1

I'm trying to send data from my React app using the Fetch API to a server. For now, I just would like to see what values are actually being sent to the server. When I try to do a var_dump($_POST) nothing happens even when the response status is 200.

React App

sendToPHP = cssObject => {
    let json_str = JSON.stringify(cssObject);
    json_str = encodeURIComponent(json_str);

    fetch("/includes/modules/CertBuilder/api.php", {
        method: "POST",
        body: json_str
    }).then(res => {
        console.log(res);
    });
}

api.php

<?php
require_once('../common.php');
var_dump($_POST) ;

I'm also using Qcodo which I realize is not a common framework these days. Plain PHP is also fine. Any help is appreciated. Thanks!

NoobNewb
  • 583
  • 2
  • 10
  • 21
  • I'm not terribly familiar with React but I imagine you need your api.php to return JSON -- which the output of `var_dump()` isn't... – Alex Howansky Sep 16 '19 at 15:29
  • @AlexHowansky Its javascript more or less. I've tried using GET on the Fetch API and it displays the content fine. The problem is I don't want the data to be on the URL. – NoobNewb Sep 16 '19 at 15:38

1 Answers1

0

Add a catch-block to debug your code:

sendToPHP = cssObject => {
    let json_str = JSON.stringify(cssObject);
    json_str = encodeURIComponent(json_str);

    fetch("/includes/modules/CertBuilder/api.php", {
        method: "POST",
        body: json_str
    })
    .then(res => {
        console.log(res);
    })
    .catch(error => console.error(error));;
}
Mischa
  • 1,591
  • 9
  • 14