如何解决“在CAKEPHP 2.x中禁止的ajax 403错误” [英] How to resolve "ajax 403 error forbidden in CAKEPHP 2.x"
问题描述
我试图在cakephp中使用ajax调用访问数据,但获得403禁止的错误,如下所示。
GET http :// localhost / ec / bazar / Products / color_switcher / 3
403(禁止)
jquery.js:6
x.ajaxTransport.sendjquery.js:6
x。 extend.ajax53:251
colorSwitcher53:327
onclick
这个代码来调用
function colorSwitcher(id){
var testid = id;
$ .ajax({
type:'GET',
url:'<?php echo $ this-> webroot;?> Products / color_switcher /'+ testid,
错误:function(){
console.log(ajax call中的错误);
},
success:function(data){
$ img-portion)。html(data);
},
});
}
控制器
$ b b
public function color_switcher($ testid =''){
$ this-> layout ='ajax';
}
color_switcher.ctp
<?php echohelooooooo; ?>
p>
如果您使用Auth,则需要确保您已登录,如果控制器/操作不在您的 $ this-> Auth - >允许()
列表。
b
public function beforeFilter(){
parent :: beforeFilter();
$ this-> Auth-> allow('action name');
}
在我的例子'action name'将是color_switcher
确保您将debug设置为0,可能会导致一些问题。
解释其中由@Dunhamzzz
i'm trying to access data using ajax call in cakephp but getting 403 forbidden error, given below.
GET http://localhost/ec/bazar/Products/color_switcher/3
403 (Forbidden)
jquery.js:6
x.ajaxTransport.sendjquery.js:6
x.extend.ajax53:251
colorSwitcher53:327
onclick
i'm using this code to make call
function colorSwitcher(id){
var testid = id;
$.ajax({
type: 'GET',
url: '<?php echo $this->webroot; ?>Products/color_switcher/' + testid,
error: function () {
console.log("error in ajax call");
},
success: function (data) {
$("#img-portion").html(data);
},
});
}
controller
public function color_switcher($testid = ''){
$this->layout= 'ajax';
}
color_switcher.ctp
<?php echo "helooooooo"; ?>
After a lot of struggle i find out solution
If you are using Auth, you need to make sure that you are logged in if the controller/action is not on your $this->Auth->allow()
list.
or simply make it allow for public access by
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('action name');
}
in my case 'action name' will be color_switcher
Make sure you set debug to 0 as well, might cause you some problems.
explained here by @Dunhamzzz
这篇关于如何解决“在CAKEPHP 2.x中禁止的ajax 403错误”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!