Allow send and receive data in a request prepared with yii2 ajax request
Include the script into head from your page.
Ex.:
<script src="yii2AjaxRequest.js" />
Case your ActiveForm not contain a id defined from you, set.
Ex.:
<?php
$form = ActiveForm::begin(['id'=>'my-form']);
...
ActiveForm::end();
?>
After, add this call into final of your page (Or use registerJs):
<script>
//#my-form => The id or class of your form
//const configs = {}; // A object of configs
yii2AjaxRequest('#my-form',configs,
(success) => {// The responde of request.
},
(error) => {// The return of a block try / catch
});
</script>
When your form as submited, the request is sended by ajax.
Case you need clear the form when finished request, add this key in your object config:
const configs = {resetForm:true};
To add a message into button submit when the request is processed, add this key in your object config:
const configs = {messageLoading:'Loading'};
The object "success" return this content:
{
data: {id:1233,name:"User",...} // The response of you method called
status: 201,200,500... // The status of request
}
The object "err" return the content of a exception
- Rony Silva - Initial work