用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3578|回复: 6
收起左侧

[讨论/交流] codeigniter中使用jquery的post时出错

[复制链接]
发表于 2011-7-24 12:26:25 | 显示全部楼层 |阅读模式
本帖最后由 itfans 于 2011-7-24 13:03 编辑

我使用的是codeigniter2.0和jquery1.6.1
js
$.post("http://localhost/develop/auth/is_email_available",{"email":"some@example.com"},
           function(data){alert(data);});
php
function is_email_available(){
echo 'true';
}
结果无法alert.
如果使用$.post("http://localhost/develop/auth/is_email_available","some@example.com",
           function(data){alert(data);});
就可以,但此时如何在php获得获得email呢?
是不是因为jquery自动生成查询字符串,而CI默认禁用了查询字符串?
发表于 2011-7-25 09:13:34 | 显示全部楼层
不可能,肯定是你哪儿用的不对
 楼主| 发表于 2011-7-26 10:32:02 | 显示全部楼层
但是我不知道哪里出了错,求高手指点!
 楼主| 发表于 2011-7-26 10:32:47 | 显示全部楼层
jeongee 发表于 2011-7-25 09:13
不可能,肯定是你哪儿用的不对

能提几个建议吗?
发表于 2011-7-26 10:53:09 | 显示全部楼层
itfans 发表于 2011-7-26 10:32
能提几个建议吗?

建议就是你用chrome或者firefox的firebug等开发工具看看是哪里出错了呗
 楼主| 发表于 2011-7-27 23:33:02 | 显示全部楼层
本帖最后由 itfans 于 2011-7-27 23:35 编辑

找到错误所在了。
我开启了csrf_protection
$config['csrf_protection'] = TRUE;
此时,CI会在表单中添加一个隐式表单:
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>" />
在使用jquery的ajax发送数据时,如果不包含该表单的值,出于安全,该操作会被禁止。
将csrf_protection设为FALSE,就可以使用了。或者,每次进行post时,均传递上述表单值也行。
参考:http://codeigniter.com/forums/viewthread/163976/
发表于 2012-8-24 10:30:46 | 显示全部楼层
感谢楼上的

本版积分规则