Providing a JSONP service

Configure your IBM® App Connect Enterprise message flow to provide a JSONP service response.

Before you begin

Before completing this task, read the following overview topics about JSON:

About this task

You can use ESQL or Java™ to configure your message flow to provide a JSONP response.
The code examples in this task assume that the client application provides a JavaScript call in the following format:
<script type="text/javascript"
 src="http://brokerhost:7080/flowUrlPathSuffix?jsonp=scriptFn">
</script>

Procedure

  1. On the Advanced tab of your HTTPInput node, select Parse Query String.
    This option enables you to access the JSONP script prefix that is included in the incoming URL, for example scriptFn, from the local environment tree.
  2. Insert the following code, as appropriate:
    • If your message flow uses a Compute node:
      SET OutputRoot.JSON.Padding = InputLocalEnvironment.HTTP.Input.QueryString.jsonp;
      SET OutputRoot.JSON.Data.objectName = 'thing1';
    • If your message flow uses a JavaCompute node:
      MbMessage outMessage = new MbMessage();
      MbElement outRoot = outMessage.getRootElement();
      MbElement outParser = outRoot.createElementAsLastChild(MbJSON.PARSER_NAME);
      String paddingString = 
          assembly.getLocalEnvironment().getRootElement().getFirstElementByPath("HTTP/Input/QueryString/jsonp").getValueAsString();
      MbElement padding = outParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Padding", paddingString);
      MbElement data = outParser.createElementAsLastChild(MbElement.TYPE_NAME, "Data", null);
      data.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"objectName","thing1");
    This code generates the following bit stream, sent as the HTTP reply:
    scriptFn( {"objectName":"thing1"} )

    This bit stream causes the JavaScript function scriptFn to be called with the JSON object as a parameter.