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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助