Php CodeIgniter-对控制器的Ajax Json调用不工作
我目前正在使用CodeIgniter 2.2.2 我有以下控制器代码:Php CodeIgniter-对控制器的Ajax Json调用不工作,php,ajax,codeigniter,Php,Ajax,Codeigniter,我目前正在使用CodeIgniter 2.2.2 我有以下控制器代码: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class switchLang extends CI_Controller { public function __construct() { parent::__construct(); $this-
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class switchLang extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function changeLang()
{
log_message('debug', 'INSIDE');
//echo "test";
$data = array("STATUS"=>"true");
//echo "HI";
echo json_encode($data) ;
}
public function index(){
}
}
我认为可能存在报价问题变更:
var base\u url=''代码>
到
var base\u url=''
在ajax调用中,base\u url
的值是多少?/switchLang/changeLang
在上面的url中重复。这是正确的URL吗?这就是我的意思:似乎我在codeigniter中的URL总是有问题。但在这种情况下,我不认为我的url是错误的。因为我试着在我的浏览器中复制和粘贴url,它工作得非常好,没有像alredy建议的那样重新加载。由于您并没有向控制器发布任何内容,为了测试起见,请尝试使用AJAX GET类型来查看结果。这两行之间的区别是什么?jconsole日志中按类型显示的内容console.log(数据)代码>?
<script type="text/javascript">
$(document).ready(function() {
var base_url = '<?php echo site_url('switchLang/changeLang');?>';
$('#lang').click(function(event) {
console.log(5 + 6);
$.ajax({
'async': false,
'url' : base_url,
'type' : 'POST',
'dataType': 'json',
'data' : 'data',
'success' : function(data){
console.log(data);
if(data){
location.reload();
}
}
});
});
});
</script>