Select Page

Server Side script:

var MyScriptInclude = Class.create(); MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  myFunction : function() {                    
         var obj = {};
         obj.var1 = 'Hello';
         obj.var2 = 'World';

         var json = new JSON();
         var data = json.encode(obj);//JSON formatted string                       
         
         return data;
  },

  type: 'MyScriptInclude'

});

Client side Script:

function onLoad() {           
         var ga = new GlideAjax('MyScriptInclude');           
         ga.addParam('sysparm_name', 'myFunction');           
         ga.getXML(showMessage); 
}   

function showMessage(response) {           
         var answer = response.responseXML.documentElement.getAttribute("answer");         
         g_form.addInfoMessage(answer); //JSON String             
         
         // answer = answer.evalJSON(); Not use that, it is no longer supported !
         answer = answer; //Transform the JSON string to an object           
         g_form.addInfoMessage(answer);             

}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Now we need to call the same Script Include from a Client Script and Server Script. in that case we can use the script bellow 🙂

var MyScriptInclude = Class.create(); 
MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	myFunction : function(x0){
		var results = {};
		var x = this.getParameter('sysparm_x') || x0;
		gs.info(x);
		results.message = 'success';
		return JSON.stringify(results);
	},
	
    type: 'My_Functions'
});