Added feature #11819: Added inline Javascript rendering type for TYPO3AJAX
[Packages/TYPO3.CMS.git] / typo3 / classes / class.typo3ajax.php
index 3b865d3..1414567 100644 (file)
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 2008 Benjamin Mack <mack@xnos.org>
+*  (c) 2008-2009 Benjamin Mack <mack@xnos.org>
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -142,7 +142,7 @@ class TYPO3AJAX {
         * @return      void
         */
        public function setContentFormat($format) {
-               if (t3lib_div::inArray(array('plain', 'xml', 'json', 'jsonhead', 'jsonbody'), $format)) {
+               if (t3lib_div::inArray(array('plain', 'xml', 'json', 'jsonhead', 'jsonbody', 'javascript'), $format)) {
                        $this->contentFormat = $format;
                }
        }
@@ -186,6 +186,9 @@ class TYPO3AJAX {
                        case 'json':
                                $this->renderAsJSON();
                                break;
+                       case 'javascript':
+                               $this->renderAsJavascript();
+                               break;
                        case 'xml':
                                $this->renderAsXML();
                                break;
@@ -203,6 +206,7 @@ class TYPO3AJAX {
         * @return      void
         */
        protected function renderAsError() {
+               header(t3lib_div::HTTP_STATUS_500 . ' (AJAX)');
                header('Content-type: text/xml; charset='.$this->charset);
                header('X-JSON: false');
                die('<t3err>'.htmlspecialchars($this->errorMessage).'</t3err>');
@@ -258,6 +262,25 @@ class TYPO3AJAX {
                        echo $content;
                }
        }
+
+       /**
+        * Renders the AJAX call as inline JSON inside a script tag. This is useful
+        * when an iframe is used as the AJAX transport.
+        *
+        * @return       void
+        */
+       protected function renderAsJavascript() {
+               $content = '<script type="text/javascript">
+                                       /*<![CDATA[*/
+
+                                       response = ' . json_encode($this->content) . ';
+
+                                       /*]]>*/
+                                       </script>';
+
+               header('Content-type: text/html; charset=' . $this->charset);
+               echo $content;
+       }
 }