Answer the question
In order to leave comments, you need to log in
Why is the data from the server not in json format?
Hello.
I send the modal form data to the server like this:
$('body').on('submit', '.modal-form', function (e) {
e.preventDefault();
var form = $(this);
$.ajax({
url: '" . Url::to(['submitmodal']) . "',
type: 'POST',
data: form.serialize(),
success: function (result) {
setTimeout(function() { $('#my-modal').modal('hide');}, 500);
var id = result.id;
var type = result.type;
$('#dropDownList-types').append('<option value='+id+'>'+type+'</option>');
},
error: function () {
alert('error in ajax modal submit');
}
});
});
public function actionSubmitmodal()
{
$model = new Typecostauto();
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if ($model->load(Yii::$app->request->post())) {
if ($model->save()) {
return ['id' => $model->id, 'type' => $model->Type];
}
else
return ['id' => 'fail'];
}
else
return $this->renderPartial('modal', [
'model' => $model,
]);
}
Answer the question
In order to leave comments, you need to log in
1. Why renderPartial and not renderAjax?
2. To put your render into json, you need to do this:
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$myvar = $this->renderAjax('modal', [
'model' => $model,
]);
return yii\helpers\Json::encode($myvar);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question