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

时间:2013-04-11 10:27:37

标签: php jquery ajax codeigniter

在php页面中,我有一个ajax jquery代码,它调用我的php控制器来执行特定的操作,当action返回成功时,我正在刷新特定的div。

我怀疑是当我从我的控制器到我的jquery函数的共鸣时,我想要执行其他动作,例如。如果用户未登录,那么我想发出alert("Please login to continue");

之类的提醒

我已尝试过以下代码,但它根本不起作用。请帮忙

  <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>

在我的控制器中

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";
    }
} 

3 个答案:

答案 0 :(得分:0)

Yu可以在修剪空间后尝试

  success: function (html) 
           { 
                html = $.trim(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);}
                }

答案 1 :(得分:0)

You need to store response inside body first. Then You can check it.

<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) 
            { 
                $('#responseStore').html(html);

                if($('#responseStore').text() == 'not_logged_in')
                { 
                    alert("You need to login to vote.");
                }
                 if($('#responseStore').text() == 'own_question')
                { 
                    alert("You can not vote for your own question");
                }
                else{ //when success refresh the div
                    $('#questvotediv').html(html);}
            }
        });
        return false;
    });
</script>
<div style="display:none;" id="responseStore"></div>

答案 2 :(得分:0)

可能是在PHP文档中有一些在echo命令之前或之后输出的空格。如果是这样的话你的比较可能会很混乱。 由于这种情况,我通常更喜欢使用INT,因为在你的情况下,你可以将PHP的返回值设置为0表示未登录,1表示自己的问题然后在你的JS比较中只需使用parseFloat()函数和comparte INT的