查看 ci-ajax-csrf-problem 上的解决方案后我在脚本中添加了以下行,它工作正常。
var post_data = {
'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
}
插入
$.ajax({
url: '<?php echo base_url()."ajax/test";?>',
type:'POST',
dataType: 'json',
data: post_data,
谢谢大家的帮助:)
我是 Ajax/Jquery 新手,正在遵循 jorge torres 中的 Ajax for CodeIgniter 指南在我的网站上实现一个简单的 ajax 调用并遇到了问题。
我创建了一个 Ajax Controller ,这是代码片段。
class Ajax extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
$output_string = 'This is a test';
echo json_encode($output_string);
}
public function test2(){
$this->load->view('test.php');
}
}
这是该 Controller 的 View ,它与教程中的 View 相同,只是我添加了加载 url 帮助程序 $this->load->helper('url');在第一行
这是脚本代码的片段。
#getdata 是按钮类型,#result_table 是 div
$('#getdata').click(function(){
$.ajax({
url: '<?php echo base_url().'ajax/test';?>',
type:'POST',
dataType: 'json',
success: function(output_string){
$('#result_table').append(output_string);
} // End of success function of ajax form
}); // End of ajax call
});
我可以成功访问localhost.com/codeigniter/ajax/test2,但是当我单击按钮时,没有任何反应。
我尝试查看页面源信息,网址是正确的
$.ajax({
url: 'http://localhost/codeigniter/ajax/test',
type:'POST'
....
直接访问localhost/codeigniter/ajax/test也是可以的,它会显示输出消息。
我正在使用 CodeIgniter 2.1.3,我的本地主机在 XAMPP 1.7.3 上运行
提前谢谢你:)
最佳答案
查看 ci-ajax-csrf-problem 上的解决方案后我在脚本中添加了以下行,它工作正常。
var post_data = {
'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
}
插入
$.ajax({
url: '<?php echo base_url()."ajax/test";?>',
type:'POST',
dataType: 'json',
data: post_data,
谢谢大家的帮助:)
关于php - CodeIgniter,Ajax 调用未进入 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15334244/