简体   繁体   中英

how can i insert data using javascript fetch api

I am trying to get data to form my view page using fetch

Result:

var_dump($_POST) then result show array(0) {} mean empty array

 function newCustomerAdd(){
  url = "data.php";
  const x = {
      name : "hasan",
      roll : 1
  }
 fetch(url,{
     method : "POST",
     body : JSON.stringify(x),
     headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json'
     },
 }).then(function(response){
     return response.text();
 }).then(function(text){
    console.log(text);
 }).catch(function(error){
    console.log(error);
 })

 }

But var_dump($_POST) then result show array(0) {} mean empty array

My question how can I get data to from my view page

JavaScript Code

  function newCustomerAdd(){
  url = "data.php";
  let x = {
      name : "hasan",
      roll : 1
  }
 fetch(url,{
     method : "POST",
     body : JSON.stringify(x),
     headers: { 
        'Content-Type': 'application/text'
     },
 }).then(function(response){
     return response.text();
 }).then(function(text){
    console.log(text);
 }).catch(function(error){
    console.log(error);
  })
 }

PHP code Just get data

$content = json_decode(file_get_contents("php://input"));
print_r($content);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM