[FLOW3-general] Ajaxable Actions / JSON as content-type

David Schoen david-schoen at gmx.de
Mon Sep 19 01:16:18 CEST 2011


Hi Ferdinand,

thank you very much for your reply, that helped me alot. It took me some 
time now to dig into the FLOW3 sources concerning Json. I'm not sure why 
I didn't find the JsonView on my codesearch before, should have found 
it, sorry.

I tried implementing it and got some interesting behavior:

1. If I use exactly the lines you provided with a call from jQuery I 
won't get Json but html. My guess is that's because jQuery sets the 
header "Accept: application/json, text/javascript, */*; q=0.01". I'm not 
able to tell jQuery to use just "Accept: application/json". Shouldn't 
the controller choose the JsonView if the content-type is 
"application/json" and the accept-header like above? Otherwise nobody 
could use the JsonView with a jQuery.getJson-request and that would be 
sad :)

2. If I set the $supportedFormats to "json" only I would expect the 
action to use the JsonView. But it uses the TemplateView. The only way I 
can get the action to return Json in response to a jQuery-request is to 
set the $defaultViewObjectName to the JsonView and the $supportedFormats 
to "json" only.

So far I got Json working (thanks again), but can't find a way to get 
html and json in one controller because of the behavior described above.

Does this behavior make sense? I would prefer a change (like mentioned 
above) to make the JsonView work with an ordinary 
jQuery.getJson-request. I could even provide a patch. If the behavior is 
necessary the way it is could someone enlighten me? :)

Best regards,
David



Am 18.09.11 17:25, schrieb Ferdinand Kuhl:
> Hi David,
>
> just add the following two lines to your controller:
>
> protected $supportedFormats = array("html", "json");
> protected $viewFormatToObjectNameMap = array(
> 	"json" =>  "\TYPO3\FLOW3\MVC\View\JsonView",
> 	"html" =>  "\TYPO3\Fluid\View\TemplateView"
> );
>
> And have a look at the really well documented JsonView-Class. (You can
> configure the depth the objects are rendered)
>
> The controller automatically detects what the request accepts, so if your
> ajax-call (for example per jquery) explicitly asks for json, your controller
> will answer with json.
>
> Have a nice sunday,
> Ferdinand
>
> David Schoen wrote:
>
>> Hello List,
>>
>> can anyone tell me how to make an action ajaxable? I would like to call
>> an action with an JSON-request and would like to get the answer in JSON
>> so I would have to change the content-type.
>>
>> Here is what I imagine:
>>
>> /**
>> * Returns some json-object
>> * @someAnnotationToSetTheReturnTypeToJSON
>> * @return void
>> */
>> public function jsonAction() {
>> ...
>> return<someJsonObject>;
>> }
>>
>> I can't find anything about that in the documentation or the
>> blog-example. If you have any clue, tip or link, please answer :)
>>
>> Thanks in advance,
>> David
>



More information about the FLOW3-general mailing list