We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44608
    • 12 Posts
    Hi

    I'm a Joomla dev, new to MODx, and I like how MODx is designed.

    I may be wrong but I didn't find how to use AJAX in the official Documentation, a part from brief description saying that AJAX can be done with Connectors & Processors. I've seen posts & few examples of MODx-AJAX here, but I think this should deserve more space, and covered in the Documentation, as MODx has done well for other topics.

    Many thanks
      • 4172
      • 5,888 Posts
      The easiest way to create an ajax-connector, is to create a resource with a blank template and put a snippet-call on that resource, which does the logic.

      Then call the url of this resource as ajax-url.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 44608
        • 12 Posts
        Hi Bruno

        Thanks for your feedback. I'll try that.
        If the Resource act as a Connector, then the Snippet should be the Processor, right?

        Is there any particular reason why this is not discussed in the official Doc? I understood that MVC^2 is the design pattern adopted by MODx, but I'm not sure why this is not elaborated further in the documentation since AJAX is pretty important in my view.

        Is there any security risk on calling the ajax-url through the Resource > Snippet?
        eg, having any parameters visible in the URL.

        Many thanks.
          • 4172
          • 5,888 Posts
          A quick Google-Search with 'modx ajax' shows this nice example at the docs in the results:

          https://docs.modx.com/revolution/2.x/case-studies-and-tutorials/loading-pages-in-the-front-end-via-ajax-and-jquery-tabs

            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 44608
            • 12 Posts
            Hi Bruno

            Thanks for your feedback, I know that page.
            But that doesn't answer my questions.

            Hope someone will

            Many thanks,
              • 3749
              • 24,544 Posts
              It is possible to create connectors and processors (or use the MODX index.php in the connectors directory), but I usually don't bother.

              A typical AJAX call handler is just some code at a URL that does something and then returns a JSON string.

              A MODX resource with no template and nothing in the content but a call to a snippet works nicely.

              The snippet gets the incoming data (if any) from the $_GET or $_POST array, does its thing, then returns the JSON string expected by the JavaScript that made the AJAX call.

              The snippet should have a permission test at the top to make sure the current user is logged in and has permission to operate the processor.

              if (!$modx->user->hasPermission('some_permission')) {
                 /* Bail out */
              }
              
              /* Do something here */
              
              /* Create $result array here */
              
              return $modx->toJSON($result);
              
              



                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 44608
                • 12 Posts
                Hi Bob

                Thanks for your help, it make sense to me.

                Kind regards,