X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   How to properly perform AJAX (https://forum.x-cart.com/showthread.php?t=75824)

Alex L 11-29-2017 10:29 PM

How to properly perform AJAX
 
I am having trouble with using AJAX. I have controller class derived from AAdmin with following action:

Code:

        protected function doActionMyAction()
        {
            $list = \XLite\Core\Request::getInstance()->idlist;

            echo $list;
        }


I also have this piece so that I do not get forbidden thrown at me when I attempt to call that action:

Code:

    public static function defineFreeFormIdActions()
    {
        return array_merge(
                    parent::defineFreeFormIdActions(),
                    array('myaction')
        );
    }


In my javascript file I have this piece to perform AJAX call:

Code:

    var str = "TEST STRING";

    core.post(
            URLHandler.buildURL({ target: "crosscheck_order", action:
            "myaction" }),
            function (XMLHttpRequest, textStatus, data, valid) {

                console.log(data);

            },
            { idlist: str}
        );


Instead of getting value of "str" in my console, I instead get raw html of the entire default template for this controller. Any tips on what I am doing wrong are very appreciated!

qualiteam 11-30-2017 01:44 AM

Re: How to properly perform AJAX
 
Please check how \XLite\Controller\Customer\Rest and \XLite\Controller\Customer\Autocomplete controllers return JSON data.
I think this should help.

Alex L 12-01-2017 08:01 PM

Re: How to properly perform AJAX
 
Quote:

Originally Posted by qualiteam
Please check how \XLite\Controller\Customer\Rest and \XLite\Controller\Customer\Autocomplete controllers return JSON data.
I think this should help.


Thank you for your reply! I completely forgot about adding headers, but adding them did not solve my issue. I still kept receiving raw html of page on which my script was being executed. I solved this by using jQuery.ajax() post method instead and it worked just fine. But I still do not understand why I could not get core.post() to work.


All times are GMT -8. The time now is 05:47 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.