I have a problem, I make the connection to my action from the Javascript through AJAX and send the data by POST. I declare the URL "/ controller / action" and it does not connect to the action, and AJAX gives me "parsererror". The strange thing is that I did one the same but to receive of an equal action and it did not give me error and I worked well
My JS File
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);
}
});
}
My action in controller
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
}
}
}
}
My settings for URLs
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
],
],
You are sending a request to a URL with a hypen (-
), while the regex you are using for the route is the w
character, which according to regex101 is used for the following:
\w
Matches any letter, digit or underscore. Equivalent to [a-zA-Z0-9_]
Change the URL to save_account, and it will work.