- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2019 09:25 AM
I cannot find any documentation related to error handling when using GlideAjax. Not even documentation on the type of object that is passed to the function passed to the getXML method. Upon further inspection I did manage to find status and statusText fields, which contain 200 and OK EVEN if the server-side script throws an exception. For now the only way to capture a server-side error is to check if the answer is null, which is not documented. Besides, valid answers might be null for some apps, which would be mistakingly interpreted as a server error.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- 2,344 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2019 11:58 AM
You are right: the error handling of GlideAjax is almost undocumented.
If you want to handle Ajax error then you can do use setErrorCallback method of GlideAjax to set errorCallbackFunction property of GlideAjax object. The callback will be called by internal _handleError method if the request will be finished with the status code not equal 200. The corresponding code example will be about the following:
var ga = new GlideAjax("SomeClientCallableServerClass");
ga.addParam("sysparm_name", "someMethod");
ga.addParam("sysparm_some_param", 12345);
ga.setErrorCallback(function (reqest) {
// the code will be executed on error
});
ga.getXMLAnswer(function (answer) {
// the code will be executed on success
});
where request is the wrapper on XMLHttpRequest or new ActiveXObject("Microsoft.XMLHTTP") object.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2019 11:06 AM
Is there a question here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2019 11:58 AM
You are right: the error handling of GlideAjax is almost undocumented.
If you want to handle Ajax error then you can do use setErrorCallback method of GlideAjax to set errorCallbackFunction property of GlideAjax object. The callback will be called by internal _handleError method if the request will be finished with the status code not equal 200. The corresponding code example will be about the following:
var ga = new GlideAjax("SomeClientCallableServerClass");
ga.addParam("sysparm_name", "someMethod");
ga.addParam("sysparm_some_param", 12345);
ga.setErrorCallback(function (reqest) {
// the code will be executed on error
});
ga.getXMLAnswer(function (answer) {
// the code will be executed on success
});
where request is the wrapper on XMLHttpRequest or new ActiveXObject("Microsoft.XMLHTTP") object.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2019 08:43 AM
Thank you for your reply, but it seems that manually triggering an exception on the script include side does not trigger the error callback. As a mentioned, status 200 is returned by the server even if the server-side script throws an exception 😕.
PD: I wonder why GlideAjaxV3 is used as GlideAjax, but that's another topic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2019 11:13 AM
Could you describe more detailed what you mean? What you mean with "manually triggering an exception on the script include side"?
If you use for example jQuery.ajax, that you have possibility to execute some code in case of successful response from from server (mostly in case of status code 200, but it could be other) and in case of error processing (typically in case of status code >400). Success or error will be considered only on the level of HTTP protocol. In some cases small status codes, like status code 0 should be interpreted as error too, because request timeout finished with status code 0. jQuery.ajax allows specify two callback functions: one for processing of successful responses (success callback) and another one for processing of error (error callback). In the same way the callback specified in getXMLAnswer or getXML will be called on receiving of successful response from the server and the callback specified in setErrorCallback will be called in case of error.
HTTP protocol know nothing about exceptions. Typically if an unhandled exception take place on the backend then HTTP response contains HTTP status 500. If you means come exceptions in your code of in internal JavaScript code of GlideAjax/GlideAjaxV3 then you can just enclose your code fragment in try {...} catch (ex) {...} block.
If you mean that you use GlideAjax to call your own Script Include class inherited from AbstractAjaxProcessor and the method contains exception, then you have to handle that inside of your Script Include class. For example, you can enclose the main part of the code of your method in try {...} catch (ex) {...} and return results different in case of successful processing and in case of error. You can use this.setError method defined in AbstractAjaxProcessor.