CodeIgniter - 视图中没有显示任何内容

时间:2013-09-24 18:03:16

标签: ajax codeigniter

我不知道问题是什么。我有一个ajax,它将用户名发送给控制器:

function my_profile(username){

    $.ajax({

       url: "member/my_profile",
       type: "get",
       data: "username="+username,
       success: function(){
           window.location.href = 'member/my_profile';
       }
    });
} 

这是我的控制者:

function my_profile(){


    $username = $this->input->get('username');
    $data['username'] = $username;
    $this->load->view('my_profile' , $data);
} 

我已经回显了$ username以测试它可以从ajax发出警报(msg)。它只是找到了。我的观点中没有显示任何问题:

    <h1>My Profile</h1>

<?php

echo $username;
?> 

我不知道为什么。我尝试初始化$data['username'] = 'adam',这很有效。

2 个答案:

答案 0 :(得分:2)

问题在于你window.location.href = 'member/my_profile';。这会将您重定向到个人资料页面,而不会任何username值。

您可能想要这样做:

 window.location.href = 'member/my_profile?username='+username;

尽管如此,我仍然不明白你为什么要在那里进行AJAX调用。你不能这样做:

function my_profile(username){
     window.location.href = 'member/my_profile?username='+username;
}

您的AJAX通话正在加载页面然后丢弃内容,我认为您不需要它。

答案 1 :(得分:1)

$.ajax({
   url: "member/my_profile",
   type: "get",
   data: "username="+username,
   success: function(){
       window.location.href = 'member/my_profile';
   }
});

应该是:

$.ajax({
   url: "member/my_profile?username=" + username,
   type: "get",
   success: function(){
       window.location.href = 'member/my_profile';
   }
});
相关问题