无法使用Yi2通过POST方法将Javascript数据发送到操作
我有一个问题,我通过AJAX从Javascript连接到我的操作,并通过POST发送数据。我声明了URL“/controller/action”,但它没有连接到该操作,AJAX给了我“parserrror”。奇怪的是,我做了同样的一件事,但得到的是一个平等的行动,它没有给我错误,我工作得很好 我的JS文件无法使用Yi2通过POST方法将Javascript数据发送到操作,javascript,php,json,ajax,yii2-advanced-app,Javascript,Php,Json,Ajax,Yii2 Advanced App,我有一个问题,我通过AJAX从Javascript连接到我的操作,并通过POST发送数据。我声明了URL“/controller/action”,但它没有连接到该操作,AJAX给了我“parserrror”。奇怪的是,我做了同样的一件事,但得到的是一个平等的行动,它没有给我错误,我工作得很好 我的JS文件 function save_account(){ $.ajax({ url: '/user/save-account', type: 'POST', dataType:
function save_account(){
$.ajax({
url: '/user/save-account',
type: 'POST',
dataType: 'json',
data: {
'id_user': xxx,
'email': xxx,
'name': xxx,
'type': xxx
},
contentType: 'application/x-www-form-urlencoded;charset=ISO-8859-15',
async: false,
success: function (response) {
},
error : function (request, error) {
console.log(error);
}
});
}
我在控制器中的操作
public function actionSaveAccount(){
$model= new \frontend\models\ProfileForm();
if (Yii::$app->request->isAjax) {
if ($model->load(Yii::$app->request->post())){
if($profile === $model->saveProfile()){
// SAVED
}else{
// ERROR SAVED
}
}
}
}
我的URL设置
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
],
],
'urlManager'=>[
“enablePrettyUrl”=>true,
'showScriptName'=>false,
“规则”=>[
“/”=>“/视图”,
'//' => '/',
],
],
您正在向带有hypen(-
)的URL发送请求,而用于路由的正则表达式是w
字符,根据,该字符用于以下内容:
\w
匹配任何字母、数字或下划线。相当于[a-zA-Z0-9_33;
更改URL以保存您的帐户,它将起作用