1

I try to create controller that handles ajax requests.

I found out, that I have to add this to my TS config:

ajaxCall = PAGE
ajaxCall {
    typeNum = 999
    config.disableAllHeaderCode = 1
    config.metaCharset = UTF-8
    xhtml_cleaning = 0
    admPanel = 0
    10 = COA
    10 < tt_content.list.20.registration_userregistration
}

And my controller looks like this:

/**
 * JSONController
 */
class JSONController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {

    /**
     * @var string
     */
    protected $defaultViewObjectName = 'TYPO3\\CMS\\Extbase\\Mvc\\View\\JsonView';

    /**
     * action test
     *
     * @return string
     */
    public function testAction() {
        $this->view->assign('value', "001");
    }
}

This works, I get a blank page with "001" on it. But if I look at the source, there are 4 empty lines, and "001" is in the 5th line.

-empty-
-empty-
-empty-
-empty-
"001"

I have no idea why...

Marcel
  • 627
  • 7
  • 25

2 Answers2

4

I don't get, why are you using some view for rendering JSON ???

public function testAction() {
    $data = array('value'=>'001');
    return json_encode($data);
}

Of course you should set Content-type: application/json - where do you prefer - in your TS or directly in the action before return;

Other clue: maybe it's caused by tt_content (just guesing), for JSON actions it's best to include them directly via Bootstrap, first register new FE plugin in your ext_localconf.php:

\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
    'VENDORNAME.' . $_EXTKEY,
    'JsonDataPlugin',
    array('JSON' => 'test',),
    array('JSON' => 'test',)
);

And modify your TS:

myAjaxPage = PAGE
myAjaxPage {

  typeNum = 999
  10 = USER
  10 {
    userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
    extensionName = Yourextname
    pluginName = JsonDataPlugin
    vendorName = VENDORNAME
  }

  config {
    disableAllHeaderCode = 1
    additionalHeaders = Content-type:application/json
    xhtml_cleaning = 0
    admPanel = 0
    debug = 0
    no_cache = 1
  }

}

(don't forget to change Yourextname and VENDORNAME for your own, also clear the system cache)

Finally: Check all your files and make sure that there's no empty lines before <?php and after ?> (the best option is to remove ?> from each file - and let PHP to terminate script at the end of file). Also it can be fixed in the source of TYPO3 as described in other naswer.

Community
  • 1
  • 1
biesior
  • 55,576
  • 10
  • 125
  • 182
  • Tanks, but I tried this already and had the same problem... I dont know why I use a view for this, im new in typo3 if you have a better way, tell me. :) – Marcel Jan 13 '15 at 17:34
  • Did this, but still the same problem – Marcel Jan 14 '15 at 07:14
  • And how about normal page? it has empty lines too? Right? – biesior Jan 14 '15 at 07:18
  • No, " " is the first line in the source – Marcel Jan 14 '15 at 07:20
  • If I call the controller without the &type=999 there are four empty lines in the source again. – Marcel Jan 14 '15 at 07:28
  • So you have empty lines before `` somwhere there in some config file(s), try to locate them i.e. by search tool of your IDE, also make sure your files haven't BOM marker – biesior Jan 14 '15 at 07:46
  • See this topic for description, of course in your case it may be other file(s): http://stackoverflow.com/a/15522791/1066240 – biesior Jan 14 '15 at 07:52
  • Thank you, I already found out the problem... It was in another file which I included to my controller. – Marcel Jan 14 '15 at 07:54
3

Okay, I got it...

I included a file with some functions named user.php

/**
 * User service
 *
 * @var \Whmcs\Registration\Service\User
 * @inject
 */
protected $user = NULL;

In this file there were empty lines after the ?> tag. These empty lines were the problem. I deleted them and now everything works fine. :)

Marcel
  • 627
  • 7
  • 25