Unhandled exception in GlideAjax

Lisa Silvaroli
Tera Guru

I have a catalog client script that keeps giving "There is a JavaScript error in your browser console" 

The console just says "Unhandled exception in GlideAjax."

This is the code I believe it is having the issue with:

var ga = new GlideAjax('StaffInfoAjax3');
			ga.addParam('sysparm_name', 'getStaffInfo');
			ga.addParam('sysparm_staff', staffId);
			ga.addParam('sysparm_access_action', pass_action);
			ga.getXMLAnswer(response);
			ga.getXMLAnswer(function(response){
			response = JSON.parse(response);

What do I need to change to make it work? 

DScroggins
ServiceNow Employee

Hi,

 

Looks like you have a few errors in the code. Try the below and see if it works:

var ga = new GlideAjax('StaffInfoAjax3');
			ga.addParam('sysparm_name', 'getStaffInfo');
			ga.addParam('sysparm_staff', staffId);
			ga.addParam('sysparm_access_action', pass_action);
			
			ga.getXMLAnswer(function(response){
			var resp = JSON.parse(response);

                        //Do something with resp
                        });

HI David, 

 

I tried your suggestion but I am still getting the same error. Here is a bigger sample of the code: 

var ga = new GlideAjax('StaffInfoAjax3');
			ga.addParam('sysparm_name', 'getStaffInfo');
			ga.addParam('sysparm_staff', staffId);
			ga.addParam('sysparm_access_action', pass_action);
			
			ga.getXMLAnswer(function(response){
			var resp = JSON.parse(response);

                          if (resp.firstname) {
		          g_form.setValue('user_firstname', resp.firstname);

});

Originally I had the following as the if but changed to to what is above after your suggestion. Both give "There is a JavaScript error in your browser console" and the console is saying "Unhandled exception in GlideAjax."

if (response.firstname) {
g_form.setValue('user_firstname', response.firstname); 

If you use a console.log() statement are you able to see that the GlideAjax is returning a value? Replace the code with this:

var ga = new GlideAjax('StaffInfoAjax3');
ga.addParam('sysparm_name', 'getStaffInfo');
ga.addParam('sysparm_staff', staffId);
ga.addParam('sysparm_access_action', pass_action);			
ga.getXMLAnswer(function(response){
  console.log(response);

});

 

Also I'm assuming that staffId & pass_action are variables which are being declared before the GlideAjax statement?

Any chance you can show how the StaffInfoAjax3 script include (getStaffInfo method) is scripted? The script include is extending the AbstractAjaxProcessor correct and is marked as client callable?

The first few lines should look like below:

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

 

I got no response with the Console.log 

 

Here is the code from my StaffInfoAjax3

var StaffInfoAjax3 = Class.create();
StaffInfoAjax3.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getStaffInfo: function() {
		var usrId = this.getParameter('sysparm_staff');
		var usrRec = new GlideRecord('sys_user');
		usrRec.get(usrId);
		if (usrRec) {
			var district = String(usrRec.getValue('u_district'));
 			var building = String(usrRec.getValue('u_building'));
			var roomNumber = String(usrRec.getValue('u_room__'));
 			var phoneNumber = String(usrRec.getValue('phone'));
 			var firstname = String(usrRec.getValue('first_name'));
			var lastname = String(usrRec.getValue('last_name'));
			var title = String(usrRec.getValue('title'));
 			var districtDisp = usrRec.u_district.getDisplayValue();
 			var buildingDisp = usrRec.u_building.getDisplayValue();
 			var email = String(usrRec.getValue('email'));
			return new JSON().encode({u_district: district, u_districtDisp: districtDisp, u_building: building, u_buildingDisp: buildingDisp, u_room_number: roomNumber, u_phone_number: phoneNumber, firstname: firstname, lastname: lastname, title: title, email: email });
			}
		},