doucan4815 2014-07-25 15:29
浏览 66
已采纳

带有codeigniter的Jquery AJAX,总是返回错误

I am trying to write a script that will add the video currently being viewed to a database of favourites. However every time it runs, an error is returned, and nothing is stored in the database.

Here is the JQuery

$(document).ready(function() {

$("#addfav").click(function() {

 var form_data = {heading: $("#vidheading").text(), embed : $("#vidembed").text()};

  jQuery.ajax({
      type:"POST",
      url:"localhost/stumble/site/add_to_fav.php",
      dataType: "json",
      data: form_data,
      success: function (data){
          console.log(data.status);
          alert("This Video Has Been Added To Your Favourites")

      },
      error: function (data){
        console.log(data.status); 
        alert("You Must Be Logged In to Do That")
      }
  });
})

})

The add_to_fav.php is this...

public function add_to_fav(){

      $this->load->model('model_users');
      $this->model_users->add_favs();    

}

And the add_favs function is below

public function add_favs(){
    if($this->session->userdata('username')){
    $data = array(
        'username' => $this->session->userdata('username'),
      'title' => $this->input->post('heading'),
      'embed' => $this->input->post('embed')
      );

      $query = $this->db->insert('fav_videos',$data);
        if($query){
      $response_array['status'] = 'success';
      echo json_encode($response_array);
        }}else {

          $response_array['status'] = 'error';
          echo json_encode($response_array);
      }

  }   

Thank you for the input, this has me stuck but I am aware it may be something relatively simple, my hunch is that it is something to do with returning success or error.

  • 写回答

1条回答 默认 最新

  • doushenyu8228 2014-07-25 16:05
    关注

    Try

    $(document).ready(function() {
    
    $("#addfav").click(function() {
    
     var form_data = {heading: $("#vidheading").text(), embed : $("#vidembed").text()};
    
      jQuery.ajax({
          type:"POST",
          url:"http://localhost/stumble/Site/add_to_fav",
          dataType: "json",
          data: form_data,
          success: function (data){
              console.log(data.status);
              alert("This Video Has Been Added To Your Favourites")
    
          },
          error: function (data){
            console.log(data.status); 
            alert("You Must Be Logged In to Do That")
          }
      });
    })
    
    })
    

    Also to use base_url in javascript. In your template view :-

    <script>
     window.base_url = "<?php echo base_url(); ?>";
    </script>
    

    Now you can use base_url in all your ajax scripts.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大