itfans 发表于 2011-7-24 12:26:25

codeigniter中使用jquery的post时出错

本帖最后由 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默认禁用了查询字符串?

jeongee 发表于 2011-7-25 09:13:34

不可能,肯定是你哪儿用的不对

itfans 发表于 2011-7-26 10:32:02

但是我不知道哪里出了错,求高手指点!

itfans 发表于 2011-7-26 10:32:47

jeongee 发表于 2011-7-25 09:13 static/image/common/back.gif
不可能,肯定是你哪儿用的不对

能提几个建议吗?

jeongee 发表于 2011-7-26 10:53:09

itfans 发表于 2011-7-26 10:32 static/image/common/back.gif
能提几个建议吗?

建议就是你用chrome或者firefox的firebug等开发工具看看是哪里出错了呗

itfans 发表于 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/

see-sea 发表于 2012-8-24 10:30:46

感谢楼上的
页: [1]
查看完整版本: codeigniter中使用jquery的post时出错