'How do I use Fetch in React?

    React Code:

    const response = await fetch("../componentsunused/testingphp.php", {

      headers: {
        "Content-Type": "application/json" // request content type
      }
      
    });
    console.log("myResponse:" + JSON.stringify(response))

    this.setState({ items: await response.json() });

    PHP Code:

    $arr = array('a' => "123");

    echo json_encode($arr);

myResponse in console always comes out as an empty array. I checked and the request went through and was returned okay. What is wrong? Thank you very much for your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source