调用名为URL的ajax在没有ajax的情况下运行良好,例如。 http://localhost/ci/controller/method/param_value。但是使用ajax它不起作用。 以下是代码段,我刚刚开始使用带有CI的JQuery + Ajax。
这是我的控制器:
<?php
类BaseController扩展Controller {
public function BaseController(){
parent::Controller();
$this->load->helper('url');
}
function index(){
$this->load->view('header');
$this->load->view('body');
$this->load->view('footer');
}
function ajaxTest($testData=""){
//$this->load->view('ajaxtest',array('test_data'=>$testData));
echo 'Got It';
}
} ?&GT;
以下是查看
<?php
echo $ test_data; ?&GT;
Ajax调用函数
function testAjaxCall(){
$.post(
base_url+'basecontroller/ajaxtest/test-value',
function(responseData) {
$("#test-div").html(responseData);
}
);
}
Ajax呼叫页面(按钮)
<div id="test-div"></div><input type="button" onclick="testAjaxCall()" name="ajax-test" value="Test Ajax" />
JS base_url声明
<script type="text/javascript" src="<?php echo base_url();?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>js/application-ajax-call.js"></script>
<script type="text/javascript">
/* to make ajax call easy */
base_url = '<?php echo base_url();?>';
</script>
哦!有趣的是,它的工作在IE8而不是FF。我不知道。
答案 0 :(得分:0)
你在JS中正确定义了base_url吗?
尝试更改此
base_url+'basecontroller/ajaxtest/test-value',
到
'<?=base_url()?>basecontroller/ajaxtest/test-value',
编辑: 另外,我强烈建议您安装Firebug(Firefox插件),以便调试Ajax帖子。
EDIT2:CI不允许通过GET请求传递参数。尝试更改$ .get(到$ .post(