1 year ago

#387161

test-img

Shizaro

Issue with fetch PHP not getting POST data

I'm using the following code :-

Javascript :-

var t_sql="SELECT * FROM `compliance_report`;";

  const options = {
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify( {sql: t_sql} )  
  };
  fetch( 'test3.php', options )
      .then( response => response.json() )
      .then( response => {
         console.log(response);
      } );

The PHP code is just to echo back the Post data

:-

<?php
$sql=$_POST['sql'];
$au=json_encode($sql);
echo $au;
?>

But all I am getting back is NULL? can anyone tell me what is wrong. I ultimately want to run the query and echo back the result but the server is reporting the $_POST as empty?

fetch-api

0 Answers

Your Answer

Accepted video resources