drg17404 2019-06-21 12:14
浏览 68
已采纳

怎么能用jj将json数据发送到php文件

ı use sweetallert js in my file and this code get some data about users but this code is not sending data to the controller.

when ı click button sweetallert function is run and ask some questions to user

Ajax code in view file

<script>
 function kitap_ekle(){
swal.mixin({
  input: 'text',
  confirmButtonText: 'Sonraki &rarr;',
  showCancelButton: false,
  progressSteps: ['1', '2', '3', '4'],
}).queue([
  {
    title: 'Kitap İsmi',
    text: 'Lütfen Kitap İsmi Giriniz',
    inputPlaceholder: 'Enter here',
   },
  {
    title: 'sayfa Sayısı',
    text: 'Lütfen Sayfa sayısı '
  },
]).then((result) => {
  if (result.value) {
   swal.fire({
 
      title: 'Blok ekleme işlemi tamalandı',
      html:
        'Your answers: <pre><code>' +
          JSON.stringify(result.value) +
        '</code></pre>',
      confirmButtonText: 'Teşekkürler!'
 
    }),
         $.ajax({
          url:'Slemler/blok_ekle',
          type:'post',
          dataType:'json',
          contentType:'application/json',
          data:JSON.stringify(result.value),
        
      }
              )
  }
})
    
}
 </script>

this is controller file

   public function blok_ekle(){
    if(json_decode($_POST["myData"])!=""){

      $data=json_decode($_POST["myData"]);
      $this->db->insert('bloklar',$data);
    } else {
        echo "no data";

    }

}
</div>
  • 写回答

1条回答 默认 最新

  • dongxu8533486 2019-06-21 13:02
    关注

    The $_POST super global is only populated for application/x-www-form-urlencoded and multipart/form-data. So for application/json it will not be populated.

    To read the json you can use file_get_contents

    public function blok_ekle(){
        if(json_decode(file_get_contents('php://input'), true)!=""){
    
          $data=json_decode($_POST["myData"]);
          $this->db->insert('bloklar',$data);
        } else {
            echo "no data";
    
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵