【问题标题】:how can i insert data using javascript fetch api我如何使用 javascript fetch api 插入数据
【发布时间】:2019-05-14 00:29:00
【问题描述】:

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

结果:

var_dump($_POST) 然后结果显示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) 然后结果显示array(0) {} 表示空数组

我的问题是如何从我的view page 获取数据

【问题讨论】:

  • 您上一个关于同一主题的问题有什么问题? stackoverflow.com/questions/56116734/…
  • 没有得到正确答案
  • 仅仅因为您在第一次尝试中没有得到预期的答案而再次发布相同的问题是不可行的。您将让主持人关闭此问题,或从您上一个问题中获得完全相同的答案
  • 我删除旧问题

标签: javascript php json codeigniter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多