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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器