Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5489

Call Ajax request in X-cart(5.3.x) template(Backend)

$
0
0

Trying to use Ajax to pass some data to PHP file from x-cart(version5.3 or above) template is causing an error (403 Forbidden Access ). So, let’s learn that how to fix that types of error in x-cart. Please follow the below example:

Step-1

Like I just want to perform an ajax request in admin view page section of x-cart on a button click that calls a function of same view page controller class. The Ajax request code is as below:

$(document).ready(function() {
        $('body').on('click','.btn_class',function(){
	        var ids = $(this).val();
		var action = $('form').attr('action');
		xyz(ids);
        });
        function xyz(ids){
            core.post(
                URLHandler.buildURL({target: 'controller_class_name', action: 'action_performed'}),
                function(XMLHttpRequest, textStatus, data, valid) {
                    if (valid && data) {
                        console.log('valid data');
                    }else{
                        console.log('Not valid');
                    }
                },
                { Ids: ids }
            );

        }
    });
Step-2

So, if I call such ajax request to target controller class then it throws a 403 Forbidden Access. To solve such types of error we must need to write a function name [ defineFreeFormIdActions()] in that controller then write your action function in that controller as below :

namespace XLite\Module\<YOUR-DEVELOPER-ID>/<YOUR-MODULE-ID>\Controller\Admin;

class ControllerClassName extends \XLite\Controller\Admin\AAdmin {
    /**
     * Define the actions with no secure token
     *
     * @return array
     */
    public static function defineFreeFormIdActions(){
        $list = parent::defineFreeFormIdActions();
        $list[] = 'action_performed';
        return $list;
    }
    protected function doActionActionPerformaed(){
        // Your Code here.
    }
}
Hope that will help you. Thank You 🙂

Viewing all articles
Browse latest Browse all 5489

Trending Articles