We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    I’m taking a class on building AJAX/PHP RIAs, and thinking a lot about best practices for AJAX: sending minimal data over the wire, using JSON, etc. I’m wondering what the best practice recommendation would be for writing the PHP scripts that respond to AJAX requests and send data.

    First thought is to create a resource... I guess it doesn’t need to use a .php extension, it could just be a standard .html resource(?)... and just include a snippet in the resource (and no template).

    Is that basically the right way to do it?

    What would be really cool is if there were a flag that could be set on snippets, so that they could be called directly by an AJAX call, to return data. The reason I say that is, it seems like wrapping a resource around a snippet that returns JSON to an AJAX call sort of seems like an unnecessary extra step. (No?)

    Just looking for some insight on this topic - overall I’m still very ignorant on this whole concept.
      • 26903
      • 1,336 Posts
      Revo(and evo) allows you to define content types that you can map to mime types such as application/json etc.

      What I’ve done in the past is to create my required content type, create a resource based on this content type, don’t assign it a template and just put your processing snippet in its content field. This is about as minimal as you can get using MODx I believe, its not quite as fast as a dedicated php script as you have the slight overhead of of going through MODx to get the page output but its worked OK for me.

      So your AJAX call just calls the page e.g. http://yoursite/index.php?id=50&parameter1&parameter2.....and you get back JSON data or whatever. Because you are using MODx you can use an alias here if set up to do so.

      The snippet doesn’t need a flag, it should just do its job, from any resource you put it in.

      There may be other ways to do this but this has worked for me.
        Use MODx, or the cat gets it!
        • 28215
        • 4,149 Posts
        What shamblett said. The reason putting it in a Resource is good is that it allows you to protect it with the inline MODx security system. That and it sanitizes your vars.

        Create a JSON (application/x-javascript) content type. Create a Resource. Assign new Content Type to it.

        Put inside it your script that handles and outputs JSON. Use $modx->toJSON and $modx->fromJSON. Point your AJAX scripts to that Resource.

        Jump for joy.
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 14883 ☆ A M B ☆
          • 450 Posts
          Good information. I sort of knew in the back of my mind that I was overlooking the content-type issue... Makes sense to use a resource to handle that.

          Is $modx->to/fromJSON always the right approach, as opposed to native PHP json_encode() & json_decode()? I guess that question probably just indicates that I haven’t fully grasped all the benefits of using the API to the max in my snippets, rather than straight PHP...
            • 26903
            • 1,336 Posts
            $modx->to/fromJSON checks for the existence of the native json_encode/decode functions, if they don’t exist it instantiates its own encoder/decoder class from the core so these should always ’just work’. Personally I tend to use the native calls as you get a bit more option on the decode(array or object if I remember rightly) and I know servers I use have these functions. I guess its up to your usage really and your platform , if in doubt use the MODx calls.
              Use MODx, or the cat gets it!
              • 15001
              • 697 Posts
              Concerning JSON, the json_encode(value) PHP function requires the value being coded in UTF-8.
              I don’t know if $modx->to/fromJSON methods do some conversion, but it’s probably wise having your database and website in UTF-8.
                • 5340
                • 1,624 Posts
                You could also create a container called ajax and store all my modx ajax pages in there.

                So if you have friendly url enabled you can have yoursite.com/ajax/get-json-data.html in your ajax call.
                  • 28215
                  • 4,149 Posts
                  Quote from: Jul at Jun 24, 2010, 05:43 PM

                  Concerning JSON, the json_encode(value) PHP function requires the value being coded in UTF-8.
                  I don’t know if $modx->to/fromJSON methods do some conversion, but it’s probably wise having your database and website in UTF-8.

                  This is because Javascript only supports UTF-8.
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com