【发布时间】:2013-03-11 08:41:34
【问题描述】:
在查看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 的 CodeIgniter 的 Ajax 指南在我的网站上实现一个简单的 ajax 调用,但遇到了问题。
我创建了一个控制器 Ajax,这是代码 sn-p。
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');
}
}
这是该控制器的视图,它与教程中的视图相同,除了我添加了加载 url 帮助器 $this->load->helper('url');在第一行
这是脚本代码的 sn-p。
#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,但是当我点击按钮时,什么也没有发生。
我试过查看页面源信息,url是正确的
$.ajax({
url: 'http://localhost/codeigniter/ajax/test',
type:'POST'
....
也可以直接访问localhost/codeigniter/ajax/test,并显示输出信息。
我正在使用 CodeIgniter 2.1.3,我的 localhost 在 XAMPP 1.7.3 上运行
提前谢谢你:)
【问题讨论】:
-
检查您的浏览器控制台是否有任何错误
-
你是否将 jquery 代码包装在 $(document).ready(function() { ... }); ..?
-
我建议您安装 firebug 或某种开发工具。检查控制台是否有错误。
-
请使用它的萤火虫工具进行测试。看看当你点击按钮时会发生什么。
-
感谢您的回复@arun-p-johny,我尝试查看浏览器控制台,它返回 500 Internal Server Error 并显示错误消息“不允许执行您需要的操作”。
标签: php codeigniter jquery codeigniter-2