在一个 php 页面中,我有一个 ajax jquery 代码,它调用我的 php 控制器来执行特定的操作,当操作返回成功时,我正在刷新特定的 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";
}
}