[英]403 - Forbidden: error when calling view file in Yii
因此,我在Yii中進行的JavaScript調用遇到了403禁止錯誤。 我正在使用XAMPP,但我不確定是什么問題。 這是我第一次在Yii中使用JavaScript / jQuery-所以我不知道是否有明顯的事情我應該改變。 許多帖子都談論過使用.htaccess的問題-但我不確定這是如何工作的,或者我將該文件放在何處。
我認為這是電話
<script>
function getBalance(){
$.get("protected/views/account/balance.php", "", function(data){
alert(data);
});
}
getBalance();
</script>
頁面balance.php的數字僅僅是7000(用於測試)。 但是,它被403(禁止)拒絕。 感謝您提供任何幫助!
在Yii中,您不能像這樣直接調用PHP文件。 您必須在控制器中設置一個動作,如下所示:
protected / controllers / CustomController.php
<?php
class CustomController extends Controller {
public function balanceAction() {
// Return a string
echo "7000";
// or, render a view file.
// This example will render protected/views/custom/index.php
$this->render('index');
}
然后必須設置控制器中的訪問控制過濾器和規則,以允許您剛剛創建的新操作,
<?php
class CustomController extends Controller {
public function filters() {
return array(
'accessControl',
);
}
public function accessRules() {
return array(
array('allow',
// add the action name in lowercase in this array
// (without the word 'action')
'actions' => array('balance'),
'users' => array('*'),
),
// deny all other actions
array('deny',
'users' => array('*'),
),
);
}
然后在您的Ajax調用或超鏈接中,您將需要使用看起來像“ controllerName / actionName”的URL來調用文件,在上面的示例中,我將使用“ custom / balance ”
$.get("custom/balance", function(){ });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.