MIDAS API Addon MIDAS API Responses

JSON

By default, the MIDAS API returns JSON (JavaScript Object Notation) formatted data. Example JSON responses are shown throughout the API Command Reference for each API call. Error responses are also returned in JSON format and denoted by the term "error".

JSONP

Optionally, the MIDAS API (from API v2.19 onwards) can return JSONP (JSON with Padding) data.

JSONP is a method commonly used to bypass the cross-domain policies in web browsers. Typically, modern browsers won't allow you to make AJAX (Asynchronous JavaScript) requests from one domain to another domain perceived to be on a different server.

For example, if your MIDAS system is running on server A, and you wish to make client-side API calls through JavaScript, you would typically only be able to make such AJAX requests from pages residing on server A itself. Attempting to initiate an AJAX request for server A from server B would be blocked by the user's browser.

JSON and JSONP behave differently on the client and the server. JSONP requests are not dispatched using the XMLHTTPRequest and the associated browser methods. Instead, a <script> tag is created, the source of which is set to the target URL. This script tag is then added to the DOM (normally inside the <head> element).

JSONP support in the MIDAS API (which is disabled by default) can be enabled via the API settings screen.

WARNING: Before enabling JSONP support, you should instead look to use CORS (Cross Origin Resource Sharing) wherever possible, as JSONP has inherent security risks as it injects JavaScript code directly into your web pages

With JSONP support enabled, you'd then be able to pass the name of a JavaScript function in a "callback" parameter along with your API calls.

Consider a simple API call to retrieve the version number of MIDAS (passing the "action" parameter with a value of "get_setting", and a "setting" parameter with a value of "version"). The typical JSON response would be:

Example JSON Response:
{"version":"4.37"}

Now consider the same API call, with JSONP support enabled, and an additional "callback" parameter passed with a value of "myfunction"). The JSONP response would then be:

Example JSONP Response:
myfunction({"version":"4.37"})

The "callback" parameter must contain the name of an existing JavaScript function on the calling page.

Upon receiving the JSONP response, the user's browser will execute the "myfunction" JavaScript function, passing the JSON data {"version":"4.37"} to it accordingly.