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 利用Java连接API接口总是出问题
  • ¥15 请教一个关于镜头标定,棋盘格格子大小的问题(畸变测试)
  • ¥15 el-table输入多维数组怎么实现
  • ¥15 安装GroudingDINO RuntimeError: Error compiling objects for extension
  • ¥15 关于推送项目到github的问题
  • ¥15 急!C++指针编写相关的问题
  • ¥15 kerberos身份认证配置问题
  • ¥30 用python写一个多签情况下波场的代理资源和回收资源
  • ¥15 怎么在matlab中输出显示泵的流量-扬程和管路损失与流量均在一个表格里
  • ¥15 matlab学期例题代码答疑