We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18409
    • 54 Posts
    What is the best way of returning JSON from a snippet to an Ajax request?

    I guess one way would be to include the snippet in a page with a blank template and point the Ajax request at that page, but it seems very messy.

    The page at:
    http://rtfm.modx.com/display/revolution20/Developer+Introduction#DeveloperIntroduction-The2ndC%3ATheConnectors
    implies I can call a connector (whatever that is) directly from an Ajax request, but I can’t find any other documentation on how this works.

    Ideally I’d like to enable direct Ajax (i.e. HTTP GET/POST) requests to certain snippets so they can essentially just load a model, turn it into JSON and send it to the browser.

    Is this possible, or is the only way to put the snippet call into a blank template...? That just seems wrong!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You don’t want all the stuff associated with a template returned to your AJAX request. That’s why the processing snippet is put in a resource with no template, so that only the snippet’s output is returned. It’s really just a convenient way of providing all of the MODx arrays and API functions as well as a valid SESSION. Otherwise you have to somehow manually start the session, include the parser and instantiate your own modx object in your PHP code.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 18409
        • 54 Posts
        Thanks, that is the recommended way to do it then? It’s how I did it in Evolution, but I was interested in this new idea of "connectors" as they sounded like a better way of connecting front end AJAX to the back end models, but couldn’t find any documentation on them. Just out of interest, what are they for?

        I guess I had imagined something like:
        GET /modx/connector/snippet_name?var=val1,...
        where MODX would load the session, modx API, permisssions etc and then run the snippet.

        I’ll continue to do it the old school way with blank templates then smiley
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          That I don’t know. I’m not at all familiar with Revo’s inner workings. These may provide some insight, although I find this comment from Jason (OpenGeek) reassuring
          The confusion is that a Resource in MODx with a Snippet that returns JSON or HTML or something, is no different than calling an external PHP "connector" for the AJAX request (except it has all the benefits of going through the MODx gateway rather than handling everything itself). And though I don’t typically use the current resource with an AJAX flag, I do use separate dedicated AJAX Resources with their own aliases and URLs for front-end AJAX work. The manager is a different animal altogether and that is where "connectors" come into play IMO.


          http://modxcms.com/forums/index.php/topic,44245.0.html
          http://modxcms.com/forums/index.php?topic=32079.0
          http://modxcms.com/forums/index.php?topic=52589.0
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 18409
            • 54 Posts
            I’ve just found the following from Jason in one of your links:
            all public AJAX interaction should be done via Resources and Snippets for components outside of the mgr context. Adding new gateways for AJAX targets is unnecessary and will potentially lead to unintended security risks.

            So that seems pretty clear!

            What I am currently doing is:
            Published page (e.g id=10), template="mainsite"
            [tt]Hello, this is the calendar
            [[+calendar]]
            [/tt]

            "Calendar" snippet:
            Displays HTML with (wait for it smiley ) the calendar!
            But there are also buttons that use jQuery to open a dialog box which then makes an Ajax request for more detail on events. My dilemma is where to point this request.

            So is the best way to create a 2nd "hidden" page (i.e. published but not linked into the main site) with a blank template, e.g. id = 11, and then change the main pages snippet call from:
            [tt][[+calendar]] [/tt]
            to
            [tt][[+calendar? & ajaxRequestUrl=`11`]][/tt]

            so it knows where to send the Ajax to? Ideally I’d like to package this for other people to use (if I ever get it working!) and it just seems like a certain amount of setup is going required by anyone installing it which I was hoping to avoid.

            Or is there anyway for a snippet to override the template the page is going to use and if the snippet detects an Ajax request then force a blank template? That would be a lot neater...