Select2 query error handling

I have chosen to use the ‘query’ method over the ajax method to load remote data. In the select2 documentation the query option isn’t as well documented but i require error handling in my plugin.

The Ajax method lacks custom error handling so i cant chose this option.

My JavaScript:

            placeholder: AJS.I18n.getText('csum.import.picker'),
            minimumInputLength: 3,
            multiple: false,
            query: function (options) {
                var url = baseUrl + "/rest/csum/1.0/space/search?atl_token=" + atlToken;

                var data = {
                    term: options.term,
                    spaceKey: currentSpaceKey,
                    pageLimit: 25

                AJS.$.post(url, data, function(results) {
            formatResult: function (object, container, query) {
                return object.spaceName + " (" + object.key + ")";
            formatSelection: function (object, container, query) {
                return object.spaceName + " (" + object.key + ")";
            id: function (item) {
                return item.key;
            text: function (item) {
                return item.spaceName;

The above code produces the following in the browser’s console:

batch.js?healthcheck-resources=true&hostenabled=true&locale=en-GB&user-logged-in=true:20 Uncaught TypeError: Cannot read property 'length' of undefined
    at i.<anonymous> (batch.js?healthcheck-resources=true&hostenabled=true&locale=en-GB&user-logged-in=true:20)
    at Object.callback (batch.js?healthcheck-resources=true&hostenabled=true&locale=en-GB&user-logged-in=true:20)
    at Object.success (batch.js?healthcheck-resources=true&hostenabled=true&locale=en-GB&user-logged-in=true:45)
    at c (batch.js?locale=en-GB:44)
    at Object.fireWith [as resolveWith] (batch.js?locale=en-GB:44)
    at u (batch.js?locale=en-GB:44)
    at XMLHttpRequest.<anonymous> (batch.js?locale=en-GB:44)

Please indicate how i’ve implemented the query method incorrectly and direct me to a solution.
Thanks in advance