繁体   English   中英

如何使用javascript fetch api插入数据

[英]how can i insert data using javascript fetch api

我正在尝试使用fetch获取数据以形成我的view page

结果:

var_dump($_POST)然后结果show array(0) {}表示空数组

 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);
 })

 }

但是var_dump($_POST)然后结果show array(0) {}意味着空数组

我的问题如何从我的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只需获取数据

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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