Submit form with ajax

I’m trying to use Ajax for submitting form with ajax without refreshing the page but I don’t succeed

my view form is :




<script type="text/javascript">


	$(document).ready(function (e) {

		$("#upload-gallery").on('submit',(function(e) {

			$(".btn-success").attr("disabled", true);

			$form = $(this);

			e.preventDefault();

			$.ajax({

				url: $form.attr('action'),

				type: "POST",

				data:  new FormData(this),

				contentType: false,

				cache: false,

				processData:false,

				//dataType: 'html',

				success: function(data)

				{

					console.log ('MAXXX');

					document.getElementById("upload-gallery").reset();

					$.pjax.reload('#post-list', {timeout : false});

				},

					error: function(){} 	        

		   });

		}));	

	});

	

</script>


<div class="post-gallery-form">


    <?php $form = ActiveForm::begin(['id' => 'upload-gallery', 'options' => ['enctype' => 'multipart/form-data']]) ?>


	<?= $form->field($model_PstGlr, 'PGalleryFile[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>

			

    <?= $form->field($model_PstGlr, 'post_text')->textarea(['rows' => 2]) ?>

	

	<?= $form->field($model_PstGlr, 'permission_id')->dropdownList($model_Permission->PermissionOn()) ?>


    <div class="form-group">

        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>

    </div>


    <?php ActiveForm::end(); ?>


</div>




My Controller is




public function actionView($chn)

    {

        $model_Permission = new Permission;

		

		$model_PstGlr = new PostGallery;	$acc_PstGlr = new AxPostGallery;

		

		if ($model_PstGlr->load(Yii::$app->request->post()) )

		{

			$acc_PstGlr->CreateGallery($model_PstGlr, $chn);

		}

		

		return $this->render('view', [

                'model' => $this->findModel($chn),

		'model_Permission' => $model_Permission,

		'model_PstGlr' => $model_PstGlr,


        ]);

    }



I have not seen function handle the ajax in your controller.

This all my code can You explain more please

An example:





if (\Yii::$app->request->isAjax) {

    // Sets the response format (in case you want json response)

    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;


    $data = \Yii::$app->request->post();

   // do something with the data


}