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.

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?