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

    I'm currently working on an addon, which uses a processor called via AJAX-Request. After the processor has finished, the user should be redirected to a new site. Normally the redirection with makeUrl + sendRedirect works fine, when I do something like this:

    $link = $modx->makeUrl(200);
    $output = $modx->sendRedirect($link);
    return $output;

    But if I use that in a processors, the user is not redirected. Any tipps?
      • 22303 MODX Staff
      • 10,725 Posts
      An AJAX request must return a response to the calling JS. You cannot redirect the browser from an AJAX request. You can however, return a location to redirect to in the JS handler for the AJAX response.
        • 16681
        • 62 Posts
        Either you do as Jason suggested and use a client side redirect, or, you put your request handler in a Snippet and go through the default MODx request process. This what I always do as I find it to be the most flexible approach.

        You should however still always wrap most of your code in classes and place them in the core/components/ folder. The Snippet should only act as a controller and reuse your neatly packaged code smiley

        To handle the ajax request, I recommend looking at my Simplx RPC Extra which is built specifically for this purpose.

        Good luck smiley
        Lars
          Keep it SIMPLX
          • 36756
          • 7 Posts
          As promised on twitter, here is the solution I chose. (using jQuery)

          1. Javascript on website
          <script type="text/javascript">
              $("#saveIcmForm").click(function() {
          	var form = $("form").serializeArray();
          	$.ajax({
          	    type: "POST",
          	    url: "[[~220]]",
          	    data: form,
          	    dataType: "html",
          	    success: function($data) {
          		var link = $data;
          		$('#popupinfo').css({'display' : 'inline'});
          		$('#popupinfo').delay(500).fadeOut();
          		if ($data) {window.location = link};
          	    }
          	});
              });
          </script>
          <div id="popupinfo">Saved</div>
          


          2. Snippet in Ressource with ID 220
          <?php
          $output='';
          //this is the processor file name
          $procname=$_REQUEST['action'];
           
          //this is what component we are referencing
          $comp=$_REQUEST['component'];
          
          //Check Authentication
          $auth = $modx->user->hasSessionContext('web');
          if ($auth == false) {
              return $modx->sendUnauthorizedPage();
          }
          
          //get the processor file with path
          $f=$modx->getOption('core_path').'components/'.$comp.'/processors/'.$procname.'.php';
          if (file_exists($f)) {
              //include that file so the output of the processor is assigned here
              $output= include $f;
          } else {
              $output= 'Action not found: '.$f;
          }
          //convert the processor output to json format
          return $modx->toJSON($output);
          


          3. Processor
          <?php
          $icmTickets = $modx->getService('icmTickets','icmTickets',$modx->getOption('icm.core_path',null,$modx->getOption('core_path').'components/icm/').'model/icm/',$scriptProperties);
          if (!($icmTickets instanceof ICM)) return '';
          $ticketId = $icmTickets->setTicket('', $_POST);
          $link = $modx->makeUrl(203, '', array('ticketId' => $ticketId)).'&function=edit';
          echo $link;
          


          The processor uses a class to write into custom database tables and returns the last database entry id. This id is used to create a URL parameter. I know it's not recommended to use "echo" and use return instead, but unfortunately: It doesn't work this way. Another thing is, that if I add the second URL parameter within the array, it creates a &ticketId=3&function=edit. I'm using FURLs on the site.

          In the form, there are to fields to address the correct processor

          <input type="hidden" name="action" value="web/icmtickets/[[getUrlParam? &name=`function`]]" />
          <input type="hidden" name="component" value="icm" />
          


          Thanks for your support, guys!
            • 16681
            • 62 Posts
            Neat smiley Thanks for sharing your solution! We need more of that stuff smiley

            Happy holidays!
              Keep it SIMPLX