Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fetch not returning data

Javascript:

fetch("https://localhost/data.php", {
  method: "POST",
  body: JSON.stringify({
    data1: data1,
    data2: data2
  })
})
.then(res => res.json())
.then(json => function (json) {
  console.log("Successful fetch.");
  console.log(json);
})
.catch((error) => function (error) {
  console.log("Failed to fetch. " + error);
});

data.php:

<?php
echo '{
    "reply": true
}';
?>

I am trying to use a post request with fetch, but I cannot get a reply back from my PHP file, but I can access it from my browser.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The error is in this line .then(json => function (json) {. This code asking the then‘s callback function to return another function

fetch("https://localhost/data.php", {
    method: "POST",
    body: JSON.stringify({
      data1: data1,
      data2: data2
    })
  })
  .then(res => res.json())
  .then(json => {
    console.log("Successful fetch.");
    console.log(json);
  })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading