Comments (8)
This issue is now mitigated with: c61941f
Until this is fixed in TYPO3 you can add the following to your view realated code:
<?php
$GLOBALS['TSFE']->setContentType('application/json');
$GLOBALS['TSFE']->config['config']['contentType'] = 'application/json';
If you do so, typoscript_rendering middleware takes care to send the correct content-type header.
from typoscript_rendering.
Hey @helhum
for future me or others who find this thread: In TYPO3 >=11 the header will be provided by returning $this->jsonResponse(). The typoscript config does not overrule a $this->htmlResponse();
from typoscript_rendering.
@manuelportela So you are saying that the content type header isn't properly set, when the plugin you are rendering is cached, but it works for uncached plugins?
Does it work for you with TYPO3 8.7?
from typoscript_rendering.
@manuelportela How do you set the content type to json?
from typoscript_rendering.
My investigation so far: With cached Extbase plugins and by setting content type with JsonView
(or doing similar within your own code), it does neither work with TYPO3 8.7 nor TYPO3 9.5, because the changed content type is not propagated to and thus not restored from cache.
This is an unfortunate shortcoming within TYPO3 and nothing that can easily be changed in typoscript_rendering
from typoscript_rendering.
sorry for the delay, I don't have a 8.7 setup for this currently around.
Actually I thought that tsr is setting the content type based on the format attribute. My endpoint is an extbase controller which uses \TYPO3\CMS\Extbase\Mvc\View\JsonView as $defaultViewObjectName.
The render method in TYPO3\CMS\Extbase\Mvc\View\JsonView is setting the content type.
/**
* Transforms the value view variable to a serializable
* array representation using a YAML view configuration and JSON encodes
* the result.
*
* @return string The JSON encoded variables
*/
public function render(): string
{
$response = $this->controllerContext->getResponse();
if ($response instanceof WebResponse) {
// @todo Ticket: #63643 This should be solved differently once request/response model is available for TSFE.
if (!empty($GLOBALS['TSFE']) && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController) {
/** @var TypoScriptFrontendController $typoScriptFrontendController */
$typoScriptFrontendController = $GLOBALS['TSFE'];
if (empty($typoScriptFrontendController->config['config']['disableCharsetHeader'])) {
// If the charset header is *not* disabled in configuration,
// TypoScriptFrontendController will send the header later with the Content-Type which we set here.
$typoScriptFrontendController->setContentType('application/json');
} else {
// Although the charset header is disabled in configuration, we *must* send a Content-Type header here.
// Content-Type headers optionally carry charset information at the same time.
// Since we have the information about the charset, there is no reason to not include the charset information although disabled in TypoScript.
$response->setHeader('Content-Type', 'application/json; charset=' . trim($typoScriptFrontendController->metaCharset));
}
} else {
$response->setHeader('Content-Type', 'application/json');
}
}
$propertiesToRender = $this->renderArray();
return json_encode($propertiesToRender, JSON_UNESCAPED_UNICODE);
}
Generally I use following snippet to disable cache in development:
[globalVar = TSFE : beUserLogin > 0]
config.no_cache = 1
config.admPanel = 0
page.config.no_cache = 1
[global]
What I did:
Viewhelper to generate URL to API endpoint:
<t:link.action pageUid="1" controller="Api" pluginName="Api" action="getLocations" format=".json">API</t:link.action>
Cached: content-type: text/html; charset=utf-8
Logged in (uncached): content-type: application/json; charset=utf-8
TRY: Additional PageType
<t:link.action pageUid="1" controller="Api" pluginName="Api" action="getLocations" format=".json" additionalParams="{type: 19922}">API</t:link.action>
Setup.typoscript
jsonPage = PAGE
jsonPage {
typeNum = 19922
config {
disableAllHeaderCode = 1
additionalHeaders = Content-type: application/json, utf-8
xhtml_cleaning = 0
admPanel = 0
debug = 0
}
}
Same result ...
from typoscript_rendering.
I was on the same track. Thank you for your quick and bright support! Is the core team aware of this shortcoming?
from typoscript_rendering.
just stumbled on this issue on a 8.7 installation. I generated an RSS feed through my extension and used a typoscript like
[globalVar = TSFE:type = 58978]
rss = PAGE
rss {
typeNum = 58978
config {
disableAllHeaderCode = 1
additionalHeaders = Content-type:application/xml;charset=utf-8
admPanel = 0
xhtml_cleaning = none
debug = 0
disablePrefixComment = 1
metaCharset = utf-8
}
}
[global]
On the first view, the content type was set correctly. On subsequent (cached) views, the content type was set to text/html.
The problem is solved by replacing
additionalHeaders = Content-type:application/xml;charset=utf-8
with
additionalHeaders.10.header = Content-Type:application/xml;charset=utf-8
I actually looked at the news extension to see why it was working there and started to recreate the configuration mentioned at https://docs.typo3.org/typo3cms/extensions/news/7.2.0/AdministratorManual/BestPractice/Rss/Index.html
The additionalHeaders form was the only part that was different than my previous configuration
from typoscript_rendering.
Related Issues (20)
- PHP 7.2 compatibility HOT 6
- Typo3 v9 compatibility HOT 5
- additionalParams doesn't work in AjaxActionViewhelper HOT 2
- "Real" json response HOT 4
- Ajax Pagination in News doesnt work HOT 6
- Additional query params HOT 4
- Is pageNotFoundOnCHashError requirement really .. required? HOT 10
- How to change the URL without reloading a page? HOT 1
- Question: version 2.1.0 and php 7.3 HOT 1
- AJAX-based pagination in the 'ext:news' works not respecting category and tag HOT 5
- uri.action and valid JSON - no "Cached page generated"
- ...\Widget\LinkViewHelper::render not compatible with ...\AbstractTagBasedViewHelper::render()
- version 2.2.0 should actually be version 3.0.0 HOT 4
- TYPO3 Security Update 10.4.14 breaks typoscript_rendering partially HOT 4
- Uri/CObjectViewHelper still uses TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper HOT 1
- TYPO3 v11 compatibility HOT 3
- PHP8 error with content-type reading HOT 2
- Ajax response always contains complete page html HOT 4
- TYPO3 v12 compatibility HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typoscript_rendering.