如何根据jquery ajax响应发出警报

In a php page , i have a ajax jquery code which calls my php controller to perform the particular action, When action returns success i am refreshing particular div.

My doubt is i i want to perform other actions when resonse come from my controller to my jquery function eg. if user is not logged in then i want to give a alert like alert("Please login to continue");

I have tried the below code but its not working at all. Please help

  <script type="text/javascript">         
        $('.voteup').click(function(e)
        {  
            // prevent the default action when a nav button link is clicked
            e.preventDefault(); 
            var $this = $(this),
            id = $this.data('id') 
            e.preventDefault(); 
            // ajax query to retrieve the HTML view without refreshing the page.
            $.ajax({
                type: 'POST',
                url:"<?php echo base_url('question/ajax_vote_up/'); ?>",
                data: {id: id},  
                dataType: 'html',
                success: function (html) 
                { 
                    if(html == 'not_logged_in')//This is not working, not giving alert 
                    { 
                        alert("You need to login to vote.");
                    }
                     if(html == 'own_question')//This is not working, not giving alert 
                    { 
                        alert("You can not vote for your own question");
                    }
                    else{ //when success refresh the div
                        $('#questvotediv').html(html);}
                }
            });
            return false;
        });
    </script>

In my Controller

function ajax_vote_up() 
{  

  $answer_id = $this->input->post('id'); 

   if ($answer_id > 0 && $this->session->userdata('logged_in') && $this->askmodel->get_user_id_By_answer_id($answer_id)!=$this->session->userdata('userid')) 
   {  
     //Few business logic related to my requirement
       $this->load->view('users/pages/show_question_By_id/votediv', $data); 
  }
   else if($this->askmodel->get_user_id_By_answer_id($answer_id)==$this->session->userdata('userid'))
  {
       //when user try to vote for their own quetion
     echo "own_question";
  }
  else
    {//Not looged in
      echo "not_logged_in";
    }
} 
查看全部
doutang7415
doutang7415
2013/04/11 10:27
  • codeigniter
  • php
  • ajax
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复