我有一个Ajax函数-
$(document).ready(function(){
$.ajax({
url: base_url+'movies/index/this_week/hindi',
type: "POST",
success: function( data )
{
$("#news1").html(data);
}
});
});
在我的电影控制器中,当我在_remap()函数中打印$this->uri->segment(2)
或$this->uri->segment(3)
时,它总是返回随机值。有时它为两个语句返回index,有时它返回'index'和'hindi'等。
当我刷新页面时,它总是返回随机值。我有点糊涂了。为什么会发生这种情况?
这是_remap()函数
function _remap()
{
$segment_1 = $this->uri->segment(1);
echo "1==>".$this->uri->segment(1);
echo " 2==>".$this->uri->segment(2);
echo " 3==>".$this->uri->segment(3);
echo " 4==>".$this->uri->segment(4);
switch ($segment_1) {
case null:
case false:
case '':
$this->index();
break;
case 'movies':
if($this->uri->segment(2) == "index" && $this->uri->segment(3) == "this_week")
{
$this->moviedescription($this->uri->segment(4));
}
else
{
$this->moviedescription();
}
break;
default:
$this->index();
break;
}
}
感谢任何帮助。提前感谢。
可能有比使用uri段更好的方法。尝试使用函数参数,如下所示:
您的movies
控制器:
public function index($when = false, $type = false) {
...
echo $when, ', ', $type;
// $when should be this_week and $type should be hindi, in your example)
// $when isn't the best variable name, but you get the idea
}
然后访问这些参数通常作为变量,因为它们总是相同的,没有随机值。我已经将它们的默认值设置为false,以防您不总是使用它们(例如,如果您有默认的movies
页面)。
我已经在我的电脑上检查了你的代码,它工作得很好。因此,如果base_url
在JAVASCRIPT中正确定义,问题不在代码中。
你可能在路由配置文件中设置了其他路由,这与url混淆了,或者你可能在.htaccess
文件中有问题。
它不应该给你随机值,那真的很奇怪,现在不确定。
当您向控制器发出ajax请求(来自您的示例)时,接收控制器应该具有:
echo $this->uri->segment(1); // result "movies"
echo $this->uri->segment(2); // result "index"
echo $this->uri->segment(3); // result "this_week"
echo $this->uri->segment(4); // result "hindi"
您可以为默认值传递第二个参数。
var_dump($this->uri->segment(5,FALSE)); // result BOOL FALSE