|
| 1 | +var ajaxWebApi = {}; |
| 2 | + |
| 3 | +/** |
| 4 | + * Wraps the jQuery ajax function with mechanisms to handle |
| 5 | + * authentication to the web API and parsing and logging of the |
| 6 | + * response object, including error handling |
| 7 | + * Parameters: |
| 8 | + * method: Web API method to call (such as midas.bitstream.list) |
| 9 | + * [authenticate]: defaults to true. If set to false, won't login |
| 10 | + * [args]: Key=value arguments to the web API method, delimited with & |
| 11 | + * [success]: Function to be called when this function is finished (one |
| 12 | + * arg, the response json object) |
| 13 | + * [error]: Function to be called if the request fails (one arg, the |
| 14 | + * response json object) |
| 15 | + * [complete]: Function to be called when done with the request, whether or |
| 16 | + * not it was successful |
| 17 | + * [log]: jQuerified DOM object representing the log area where output will |
| 18 | + * be written. Default behavior is alert() in error conditions. |
| 19 | + */ |
| 20 | +ajaxWebApi.ajax = function(params) |
| 21 | +{ |
| 22 | + if(!params.method) |
| 23 | + { |
| 24 | + alert('ajaxWebApi.ajax: method parameter not set'); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + this._webApiCall(params); |
| 29 | +} |
| 30 | + |
| 31 | +/** Internal function. Do not call directly. */ |
| 32 | +ajaxWebApi._webApiCall = function(params) |
| 33 | +{ |
| 34 | + if(!params.args) |
| 35 | + { |
| 36 | + params.args = 'useSession=true'; |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + params.args += '&useSession=true'; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + $.ajax({ |
| 45 | + type: 'POST', |
| 46 | + url: json.global.webroot + '/api/json?method=' + params.method, |
| 47 | + data: params.args, |
| 48 | + dataType: 'json', |
| 49 | + success: function(retVal) { |
| 50 | + if(params.complete) |
| 51 | + { |
| 52 | + params.complete(); |
| 53 | + } |
| 54 | + if(retVal.stat == 'fail') |
| 55 | + { |
| 56 | + ajaxWebApi.logError(params.method + '?' + params.args + ' failed: ' + retVal.message, params.log); |
| 57 | + if(params.error) |
| 58 | + { |
| 59 | + params.error(retVal); |
| 60 | + } |
| 61 | + return; |
| 62 | + } |
| 63 | + //TODO detect invalid (expired) token. If so, set our token = '' and try again |
| 64 | + if(params.success) |
| 65 | + { |
| 66 | + params.success(retVal); |
| 67 | + } |
| 68 | + }, |
| 69 | + error: function() { |
| 70 | + ajaxWebApi.logError('Ajax call to web API returned an error (' + |
| 71 | + json.global.webroot + '/api/json' + '?' + params.method + '&' + params.args + ')', params.log); |
| 72 | + if(params.complete) |
| 73 | + { |
| 74 | + params.complete(); |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +ajaxWebApi.logMessage = function(text, log) |
| 81 | +{ |
| 82 | + if(log) |
| 83 | + { |
| 84 | + log.append('<span style="color:black;">' + text + '</span><br>'); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + alert(text); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +ajaxWebApi.logError = function(text, log) |
| 93 | +{ |
| 94 | + if(log) |
| 95 | + { |
| 96 | + log.append('<span style="color:red;">Error: ' + text + '</span><br>'); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + alert('Error: ' + text); |
| 101 | + } |
| 102 | +} |
0 commit comments