簡體   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