We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46194
    • 9 Posts
    Now I am 100% certain this is just down to total lack of understanding, but I am getting a strange result from an ajax call....
    This sort of leads on from my previous post.
    I have a grid of thumbnails that are the featured image of some posts.
    I am trying to launch a modal dialog and I am trying to populate the dialog with the full post content.

    Reading through quite a bit on this forum and the web I have done the following:

    Created a resource from a blank template (showproject) and in that resource I have a call to a snippet
    [[!ShowProject]]


    The ShowProject snippet contains the following :

    <?php
    //Get the resource/project we want to show on our website
    $resource = $modx->getObject('modResource',$_POST[rID]);
    
    //The getTVValue is the ID of the template variable.
    $FeatImageOne    = $modx->resource->getTVValue(2); 
    
    //I also need the pagetitle
    $pageTitle = $modx->resource->get('pagetitle');
    
    //Finally i need the content field
    $content = $modx->resource->get('content');
    
    
    return $modx->getChunk("chunkWorkFrontModalSumm",array(
        'pageTitle'        => $pageTitle, 
        'content'          => $content, 
        'FeatImageOne'   => $FeatImageOne,
    ));


    The chunk is this:
    <div class="modal-header">
    	<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    	<h4 class="modal-title" id="myModalLabel">[[+pageTitle]]</h4>
    </div>
    <div class="modal-body">
    
    [[+FeatImageOne]]
    
    <p>[[+content]]</p>
    
    				
    </div>
    <div class="modal-footer">
    </div>


    My JS looks like this :

    	$(document).on("click", ".work-summ-modal", function () {
    			var myPageId = $(this).data('id');
    			
    		$.ajax({
    			async: false,
    			URL: 'http://www.auspiciousday.com/showproject',
    			type:	'POST',
    			data: { rID : myPageId },
    			dataType: 'html',
    			timeout: 1000,
    			cache:false,
    			error: function(XMLHttpRequest, textStatus, errorThrown){
                    $("#ShowProject").html("<p>"+errorThrown+"</p>");
    				},
    			success: function(allGood){
    				 $("#ShowProject").html(allGood);
    			}
    			
    		})	;
    					 
    		    
    	 });
    


    So the problem is and one I don't understand is that for some reason the dialog launches but just displays my home page?
    Would really appreciate some pointers or help as to where I am going wrong!

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 4172
      • 5,888 Posts
      try it this way:

      //Get the resource/project we want to show on our website
      $output = '';
      
      $rid = $modx->getOption('rID', $_POST, 0);
      if ($resource = $modx->getObject('modResource', $rid)) {
          //The getTVValue is the ID of the template variable.
          $FeatImageOne = $resource->getTVValue(2);
      
          //I also need the pagetitle
          $pageTitle = $resource->get('pagetitle');
      
          //Finally i need the content field
          $content = $resource->get('content');
      
          $output = $modx->getChunk('chunkWorkFrontModalSumm', array(
              'pageTitle' => $pageTitle,
              'content' => $content,
              'FeatImageOne' => $FeatImageOne,
              ));
      }
      
      return $output;
      
      
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 46194
        • 9 Posts
        Hi and thank you for taking the time to look at it.
        I am afraid though it just returns the same result (front page).

          • 3749
          • 24,544 Posts
          Look at the information exchanged on the Network tab of Chrome Dev. Tools (Ctrl-shift-i). That will show you a lot about what's happening with the Ajax call.

          This may help also: http://bobsguides.com/blog.html/2013/05/22/debugging-cmps-a-horror-story/ [ed. note: BobRay last edited this post 12 years, 8 months ago.]
            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
            • 10487 MODX Staff
            • 1,535 Posts
            Do you by chance have mod_security running at all? If I try and send a POST request (with rID=28) to the URL in the ajax call using an independent client (in this case, HTTPClient for MacOS) then I get the following in the response:
            HTTP/1.1 406 Not Acceptable
            Server: nginx/1.4.4
            Content-Length: 226
            Content-Type: text/html; charset=iso-8859-1
            Connection: close
            Date: Tue, 11 Feb 2014 01:37:57 GMT
            
            <head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>


            It seems strange that nginx is returned as the server, but perhaps you have nginx as a reverse-proxy? It could be that the 406 is prompting MODX to return the default error page, just speculating here.

            EDIT: I used another tool, PostMan (from the Chrome App Store), and interestingly it returns the correct content as seen here. [ed. note: garryn last edited this post 12 years, 8 months ago.]
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 4172
              • 5,888 Posts
              what about changing this:

              URL: 'http://www.auspiciousday.com/showproject',


              to this:

              url: 'http://www.auspiciousday.com/showproject',


              you can also let modx generate the url

              url: '[[~IDofResource]]',

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

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 46194
                • 9 Posts
                Thank you all for your help.

                Yes I am using Chrome Dev tools and no errors are being thrown (the digital version of your MODX guide is fantastic btw BobRay!).

                Garry, I think it is likely to be mod_security, I am on a shared hosting package (HostGator) and it is configured by them so I may not be able to do much about it right now, but I will look into it!
                • discuss.answer
                  • 4172
                  • 5,888 Posts
                  when I change URL to url and put this into my firebug - console:

                  $.ajax({
                  			async: false,
                  			url: 'http://www.auspiciousday.com/showproject',
                  			type:	'POST',
                  			data: { rID : 28 },
                  			dataType: 'html',
                  			timeout: 1000,
                  			cache:false,
                  			error: function(XMLHttpRequest, textStatus, errorThrown){
                                  $("#test-this").html("<p>"+errorThrown+"</p>");
                  				},
                  			success: function(allGood){
                  				 $("#test-this").html(allGood);
                  			}
                  			
                  		})	;
                  


                  it does work
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 46194
                    • 9 Posts
                    Quote from: Bruno17 at Feb 11, 2014, 01:13 AM
                    when I change URL to url and put this into my firebug - console:

                    $.ajax({
                    			async: false,
                    			url: 'http://www.auspiciousday.com/showproject',
                    			type:	'POST',
                    			data: { rID : 28 },
                    			dataType: 'html',
                    			timeout: 1000,
                    			cache:false,
                    			error: function(XMLHttpRequest, textStatus, errorThrown){
                                    $("#test-this").html("<p>"+errorThrown+"</p>");
                    				},
                    			success: function(allGood){
                    				 $("#test-this").html(allGood);
                    			}
                    			
                    		})	;
                    


                    it does work

                    Brilliant! Thank you very much!!!
                      • 3749
                      • 24,544 Posts
                      Yes I am using Chrome Dev tools and no errors are being thrown (the digital version of your MODX guide is fantastic btw BobRay!).

                      Thanks for the kind words and for buying the e-book. smiley

                      FYI, maybe you know this, but with Chrome Dev. tools, you can actually go deeper by clicking on the results and seeing the actual information sent in the request and the JSON string returned from the processor in the response.
                        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