ajax common code example

Example: ajax common

// common.js
var COMMON = {
    base_url: '',
    column_cache: '',
    url_update_cache: '',
    module_name: '',
    cfg_lang: '',
    action_flow: {
        approve: 'Approve',
        reject: 'Reject',
    },
    
    call_ajax: function(params){
        $.ajax({
            url: params.url,
            type: params.type,
            data: params.data,
            dataType: params.dataType,
            headers: params.headers,
            beforeSend: function(){

            },
            success: params.success,
            error: params.error,
            complete: function(){

            }
        })
    },
}

// admin_company_credit.js
var ADMIN_COMPANY_CREDIT = {
    url_get_pay_credit_detail_by_company: '',
    COMMON.call_ajax({
      url: ADMIN_COMPANY_CREDIT.url_get_pay_credit_detail_by_company,
      type: 'POST',
      data: {
        company_id: $('#company_id').val(), // params here
      },
      dataType: 'json',

      success: function(result) {
        if (result.status) {
           // succed here
        }

      },

      error: function(error) {
        console.log("error message here")
      }
    });
}

// using 

<?php  // cakephp
	echo $this->Html->script('CakeAdminLTE/pages/admin_company_credit.js?v=' . date('U'), array('inline' => false));
?>

<script type="text/javascript">
	$(document).ready(function(){
		ADMIN_COMPANY_CREDIT.url_get_pay_credit_detail_by_company 	= '<?= Router::url(array('plugin' => 'company', 'controller' => 'company_credits', 'action' => 'get_pay_credit_detail_by_company')); ?>';
		ADMIN_COMPANY_CREDIT.init_page();
	});
</script>