Error calling Script include from Client controller

maryc
Tera Contributor

Hello,

 

Here is my script include snippet and client contoller. I am getting the values printed correctly in script include, but gives Ajax error on client controller. Any help is appreciated

 

 try{
            var result = sn_fd.FlowAPI.executeAction('global.rest_api',inputs);
            var url = result.url; // String
            var statuscode = result.statuscode; // String
            //window.open(url, '_blank');
            gs.log("URL and Code " + url+ "  **  "+statuscode);
            return {
                url: url,
                statuscode :statuscode
            };
        } catch (ex) {
        var message = ex.getMessage();
        gs.error(message);
    }
c.callSterling = function() {
        c.data.requested_for = c.requestedFor.value;
        c.data.requested_by = c.requestedBy.value;
        alert(c.requestedFor.value+" @ "+c.data.requested_by);
        var ga = new GlideAjax('global.scriptincludecall');
        ga.addParam('sysparm_name', 'getRequestDetails');
        ga.addParam('sysparm_req_for', c.requestedFor.value);
        ga.addParam('sysparm_req_by', c.requestedBy.value);
       
        ga.getXML(function (response ){
            var respObj= JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
            var url= respObj.url;
        var stat= respObj.statuscode;
 
        alert(url+" "+stat);
        });
   
    }

 

1 ACCEPTED SOLUTION

Ahmmed Ali
Mega Sage

Hello @maryc 

 

In script include, you are returning the object not the JSON string. First convert object to JSON string using JSON.stringify() and then return it.

You are already parsing the JSON in client so it should work.

 

On other hand, I assume you are writing this script in a Widget. If you want to run server side script, no need use GlideAjax as you can directly call server script in Client controller in widget. Checkout below article for more details:

https://serviceportal.io/communicating-between-the-client-script-and-the-server-script-of-a-widget/ 

 

Thank you,

Ali

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

1 REPLY 1

Ahmmed Ali
Mega Sage

Hello @maryc 

 

In script include, you are returning the object not the JSON string. First convert object to JSON string using JSON.stringify() and then return it.

You are already parsing the JSON in client so it should work.

 

On other hand, I assume you are writing this script in a Widget. If you want to run server side script, no need use GlideAjax as you can directly call server script in Client controller in widget. Checkout below article for more details:

https://serviceportal.io/communicating-between-the-client-script-and-the-server-script-of-a-widget/ 

 

Thank you,

Ali

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali