The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Ajax 401 (unauthorized) error when Client Script calls Script Include from the Service Portal

Craig Jacobs
Giga Contributor

Hello Community. I have an issue with a Catalog Client Script and the Script Include the client script is calling via ajax from the Service Portal. The script works fine when I invoke it from a UI Action directly, however when calling from the Client side, I get the 401 error and nothing is returned to my callback. I have "Client Callable" selected on the Script Include and the class extends AbstractAjaxProcessor. Am I supposed to provide credentials with the Ajax call maybe? I noticed that the class initializer is being called, not sure if that's relevant.

Here is the actual error: GET https://ibmitsm.service-now.com/api/now/table/syntax_editor_macro?sysparm_fields=name%2Ctext 401 (Unauthorized)

Here are the scripts:

/* Catalog Client Script */

function onLoad() {
    try {
        var ga = new GlideAjax('PD_GetUser_Groups'); // PD_GetUser_Groups is the script include class
        ga.addParam('sysparm_name', 'getDetails'); // getDetails is the script include method
        ga.getXML(_Parse_PD_GetUser_Groups);
    } catch (err) {
        alert("PD onLoad exception: " + err);
    }
    return;

    function _Parse_PD_GetUser_Groups(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            var res = JSON.parse(answer); //JSON Parse creates an object from a java formatted string
        }
    }
}
 
/* Script include */
var PD_GetUser_Groups = Class.create();
PD_GetUser_Groups.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    type: 'PD_GetUser_Groups',
    initialize: function () {},

    getDetails: function () {
        try {
            var gr = new GlideRecord('sys_user');
            gr.addQuery('sys_id', gs.getUserID());
            gr.query();
            while (gr.next()) {
                var result = {
                    email: " ",
                    title: " "
                };
                result.email = gr.email;
                result.title = gr.title;
                var answer = JSON.stringify(result); // creates a string from JSON object
            }
        } catch (err) {
            answer = err.message;
            gs.error("PD_GetUser_Groups.getDetails exception: " + answer);
        }
        return (answer ? answer : "answer is undefined");
    },
});
 
Thank you in advance!
Craig
3 REPLIES 3

Community Alums
Not applicable

You should call it either in server side (with standart GlideRecord as in your AJAX) or do a direct HTTP request  from the  c;lient (in the widget )

Craig Jacobs
Giga Contributor

Thank you for responding, Joro. I found out that I had to add this to my class initializer:

initialize: function(request, responseXML, gc) {
   global.AbstractAjaxProcessor.prototype.initialize.call(this, request, responseXML, gc);
},

After initializing the class I extended, it seems to work.

Update: from here: ref, I found this:

1.   Delete the empty initialize function in your script include, as it will break the Abstract Ajax Processor.

Deleting the empty class initializer also worked.

Correct. Client callable Script Include does not have "initialize: function () {}" statement. This statement may exist in non-client callable Script Include.

The default template generated by ServiceNow when Client callable is checked won't generate this statement.  When in doubt, it's always best to copy the function part, delete the entire script, re-generate the template.