Typo3 extbase json output

2019-06-07 07:26发布

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...

2条回答
Deceive 欺骗
2楼-- · 2019-06-07 08:02

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. :)

查看更多
时光不老,我们不散
3楼-- · 2019-06-07 08:26

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.

查看更多
登录 后发表回答