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

    I’m trying to perform a simple ajax request and return some output. I can’t seem to get it to work. I’ve tried accessing the following URL: http://svn.modxcms.com/docs/display/revolution/MODx+Revolution+Framework+Structure+Ideology#MODxRevolutionFrameworkStructureIdeology-Connectors

    That url keeps timing out with the following message:
    Proxy Error

    The proxy server received an invalid response from an upstream server.
    The proxy server could not handle the request GET /docs/display/revolution/MODx+Revolution+Framework+Structure+Ideology.

    Reason: Error reading from remote server

    Anyway, to ask my question, what do I need to do to perform a basic AJAX request to MODx and read some data from one of my components?
      @MarkGHErnst

      Developer at Adwise Internetmarketing, the Netherlands.
      • 24865
      • 289 Posts
      I noticed that the docs are up again, but I was wondering if anybody has a decent example I can base it on?
        @MarkGHErnst

        Developer at Adwise Internetmarketing, the Netherlands.
        • 28215
        • 4,149 Posts
        Just put your JSON data (or Snippet generating the JSON data) in a Resource with a blank template (and you can create and specify a JSON content type if you wish), and point your ajax calls to it. Done.


        A similar approach is done here, with jQuery tabs:
        http://docs.modxcms.com/display/revolution20/Loading+Pages+in+the+Front-End+via+AJAX+and+jQuery+Tabs
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 24865
          • 289 Posts
          Awesome Shaun! I’m gonna try that right now. I gave up the search this morning because all links on Google redirected me to svn.modxcms.com, which was down for me.
            @MarkGHErnst

            Developer at Adwise Internetmarketing, the Netherlands.
            • 24865
            • 289 Posts
            Alright, I’ve taken a look at the JQuery tutorial which is totally not what it says on the box. At least, not for me. It’s just build in plugin that toggels a list of elements for tabs. That part is all good and well, but the data isn’t loaded with ajax, no, it’s just directly echo’d with the getField snip.

            What I have:

            • A component that outputs a multidimensional array
            • A ExtJS based request to a connector
            • A connector that so obviously doesn’t work

            What I want:
            I want to use the Ajax.request method from ExtJS to LOAD that components array through ajax/json. Now, knowing MODx, I need a connector for that, since that’s the only way to go. I love that, but I can’t seem to get it to work, which is quite troubling. See, my component works on two ways. 1) The snippet accesses the component which in turn outputs the array and 2) Ajax calls the component through a connector which then outputs the array in JSON, for me to work with in async.

            Now, this particular component fetches data through cURL, which isn’t always that reliable OR fast. So, without slowing down MODx (which would be a total waste, I can just as well go back to Joomla then) I chose to support both outputs, JSON and direct. Now, AJAX inits when the website is loaded (on(Dom)Ready). So, the user doesn’t have to wait and the data appears right where I want it to.

            Now, I’ve read some topics on the Revo 2.0 board and the documentation but I can’t really grasp it.

            This tutorial does exactly what is says on the box.
            http://svn.modxcms.com/docs/display/revolution20/Loading+Pages+in+the+Front-End+via+AJAX+and+jQuery+Tabs

            And the last section of this page goes into connectors on the absolute bare minimum...
            http://svn.modxcms.com/docs/display/revolution20/Custom+Manager+Pages

            I need some help here because I can’t work it out. For a system that is so new, there are really not that many other resources for information.

            /edit
            This was the post I meant, also by splittingred of course;
            http://modxcms.com/forums/index.php/topic,50896.msg295876.html#msg295876

            Now, this is exactly what I need, but then not via resource but through a component which can be controlled through the manager. The resources resembles a static method, and I need it to be dynamic.
              @MarkGHErnst

              Developer at Adwise Internetmarketing, the Netherlands.
              • 28215
              • 4,149 Posts
              Why not use a Snippet in a Resource?

              Loading through an external connector is a lot more work, and not necessary. The components only do it because they dont want to have to create and manage their own Resources.
                shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • 24865
                • 289 Posts
                Of course it’s a good idea to do it through a snippet, but it’s going to take a lot more than that for one of my upcoming projects. See, the resource method is perfect for small application purposes but knowing that the method to do it through a processor and connector is there and not knowing how it works kinda sucks. I can’t find any decent tutorial/article about it anywhere. Perhaps I should lay off of this until I finish a bigger portion of my own website.

                If you guys got some good articles on this subject or have any experience with it that would be greatly appreciated.
                  @MarkGHErnst

                  Developer at Adwise Internetmarketing, the Netherlands.
                  • 28215
                  • 4,149 Posts
                  You can always look at how any of the Extras that have one do it, such as Gallery, Quip, Redirector, Batcher, etc.
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 24865
                    • 289 Posts
                    Sounds like a plan. Quip is yours, isn’t it? I’ve already implemented that one into my website (still building it locally) and that looks pretty good. I haven’t seen any AJAX come into play with it tho.
                      @MarkGHErnst

                      Developer at Adwise Internetmarketing, the Netherlands.
                      • 24865
                      • 289 Posts
                      Alright, I’ve tried almost anything I could find right now and I still can’t get it to work. I now get the "Access Denied" message inside the JSON response. It feels like somewhere in the manager I need to set some access variables...

                      I’ve got the following Ext:
                      Ext.Ajax.request({
                      	url: '/modx/assets/components/mycmp/connector.php',
                       	method: 'POST',
                      	success: function(response, opts) {
                      		alert(response.responseText);
                      	},
                      	failure: function(response, opts) {
                      		alert('Fail');
                      	},
                      	params: {
                      		'action': 'fetch',
                      		'ctx': 'web'
                      	}
                      });


                      Inside this component I’ve got two locations, the assets and the core components folder. So that would be:
                      /modx/assets/components/mycmp/connector.php
                      /modx/core/components/mycmp/.*

                      Inside the connector.php file I’ve got:
                      <?php
                      
                      	$basePath = dirname(dirname(dirname(dirname(__FILE__))));
                      	
                      	require_once $basePath . '/config.core.php';
                      	require_once MODX_CORE_PATH . 'config/' . MODX_CONFIG_KEY . '.inc.php';
                      	require_once MODX_CONNECTORS_PATH . 'index.php';
                      	
                      	$modx->handleRequest(array(
                      		'processors_path' => $modx->getOption('core_path').'components/mycmp/processors/',
                      		'location' => ''
                      	));


                      Inside the core component folder from mycmp I’ve got:
                      /processors/fetch.php
                      /index.php
                      /mycmp.php

                      Now the fetch.php doc simply says:
                      <?php
                      
                      	return array('message' => 'Trying something out!');


                      Nothing fancy, but still no cigar. I do not need some quick fix by setting it into some resource and calling that, I need the core method, because a much larger project will be based on this. I’ve got the namespaces defined, I’ve done the modMenu and modAction stuff...

                      Anybody got a clue as to what I am doing wrong?

                      /edit
                      Oh yea, the return format:
                      {"success":false,"message":"Access denied.","total":0,"data":[],"object":[]}
                        @MarkGHErnst

                        Developer at Adwise Internetmarketing, the Netherlands.