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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化