0
yii2(2.0.13)を使用します。私はajaxでデータを送信すると、返信応答にエラーが含まれます。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リクエスト:
$.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