Getting FW/1 2.2 to return application/json

349 views Asked by At

FW/1 has a function buried inside of it

private string function renderDataWithContentType() {
    var out = '';
    var contentType = '';
    var type = request._fw1.renderData.type;
    var data = request._fw1.renderData.data;
    var statusCode = request._fw1.renderData.statusCode;
    switch ( type ) {
    case 'json':
        contentType = 'application/json; charset=utf-8';
        out = serializeJSON( data );
        break;

It looks like it has a build in mechanism to return application/json, text/xml, and text/plain.

I can't find any documentation on how to trigger this.

Note: this is not a duplicate of: How do I return JSON from an action in FW/1?

1

There are 1 answers

0
Sean Corfield On BEST ANSWER

You could read the documentation:

From the documentation

...

As of 2.2, you can return data directly, bypassing views and layouts, using the new renderData() function.

variables.fw.renderData( contentType, resultData );

Calling this function does not exit from your controller, but tells FW/1 that instead of looking for a view to render, the resultData value should be converted to the specified contentType and that should be the result of the complete HTTP request.

contentType may be “json”, “xml”, or “text”. The Content-Type HTTP header is automatically set to:

  • application/json; charset=utf-8
  • text/xml; charset=utf-8
  • text/plain; charset=utf-8

...