2013-04-11 10:27

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

  • codeigniter
  • php
  • ajax
  • jquery

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">         
            // prevent the default action when a nav button link is clicked
            var $this = $(this),
            id = $'id') 
            // ajax query to retrieve the HTML view without refreshing the page.
                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
            return false;

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";
    {//Not looged in
      echo "not_logged_in";
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享