Thursday, October 27, 2016

APEX4 to APEX5 Migration Tip: AJAX calls

If you're moving your APEX applications from APEX 4 to APEX 5 and you've decided that you don't want to include jQuery Migrate Plugin* (to reduce overall size of the JavaScript files loaded) as a result of your AJAX requests you may suddenly see messages like this one:

The reason of this is that there's changed behaviour of AJAX calls from jQuery version 1.9 (APEX 5 is on version 2.1.3, APEX 4 on 1.8.22). Prior to version 1.9 AJAX call that expected a return data type of JSON or JSONP would consider a return value of an empty string to be a success case, but return a null to the success handler or promise. As of 1.9, an empty string returned for JSON data is considered to be malformed JSON and it will throw an error.  

If you're using apex.server.process or apex.server.plugin for your AJAX calls by default expected return data type is JSON. So, if your AJAX callback is not returning JSON object or it's returning empty string you will get the error message like the one above.

There are two solutions to this problem:

1) Explicitly define data type in AJAX calls (for example dataType: 'text')

2) Put dummy return on end of your AJAX callback. It should be: 

  • empty JSON object - sys.htp.prn('{}')
  • or null string - sys.htp.prn('null')


*jQuery Migrate Plugin: c/p from APEX help:
The plug-in restores deprecated features and behaviors of jQuery so that old JavaScript code and jQuery plug-ins will still run properly with the jQuery version loaded by Application Express.
If you are confident your application and any used jQuery plug-in does not contain any references to deprecated jQuery features, set this to No to reduce the overall size of the JavaScript files loaded.

Tested on APEX