0
저는 yii2 (2.0.13)를 사용합니다. 아약스로 데이터를 보낼 때 반환 응답에 오류가 포함됩니다.Yii2 API 동작 변경
내 코드 :
namespace frontend\controllers;
use Yii;
use yii\rest\ActiveController;
class CityController extends ActiveController
{
public $modelClass = 'frontend\models\City';
public function actions()
{
$actions = parent::actions();
unset($actions['create']);
return $actions;
}
public function behaviors()
{
$behaviors = parent::behaviors();
// remove authentication filter
$auth = $behaviors['authenticator'];
unset($behaviors['authenticator']);
// add CORS filter
$behaviors['corsFilter'] = [
'class' => \yii\filters\Cors::className(),
];
// re-add authentication filter
$behaviors['authenticator'] = $auth;
// avoid authentication on CORS-pre-flight requests (HTTP OPTIONS method)
$behaviors['authenticator']['except'] = ['options'];
return $behaviors;
}
public function actionCreate()
{
echo 'Hi i\'m create!!';
}
}
그리고 아약스 요청 :
$.ajax({
url: "http://blog.dev/city", // our php file
type: 'POST',
contentType: false,
cache: false,
processData: false,
data: {x: 'data_text'},
success: function(data){
console.log(data);
},
error: function (request) {
console.log(request);
}
});
가 actionCreate에 내가
출구를 추가() 문제가 수정됩니다.
어디에서 문제가 발생하며 정확하게 actionCreate를 어떻게 변경해야합니까?
도와주세요.
감사하지만 작동하지 않는다면! 왜? 반응 구성 요소로부터 데이터를 보내고 _csrf를 보내지 않고도 올바르게 작동합니다. 다른 점을 따라야합니까? – MHF