get request body

shabbir9
Tera Contributor

hii

in script include "GetEnclaveData" iam getting the rest message response in responsebody(
(var responseBody = response.getBody();) i need fetch that responsebody and show it on u_comment variable can anyone correct this script where i am doing wrong in below client script i need to fetch script include responsebody onto this client script ..

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;

}
var ga = new GlideAjax('GetEnclaveData');
ga.addParam('sysparm_name','getData');
ga.getXMLAnswer(getResponse);
alert('hello');
function getResponse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert('answer');
var reqbody = JSON.stringify(answer);
alert('reqbody');
g_form.setValue('u_comment',reqbody);

1 ACCEPTED SOLUTION

@shabbir9  Please add the return statement in your script include method named getData() to get the response body when you make Ajax call from client script.

 

 

var GetEnclaveData = Class.create();

GetEnclaveData.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getData: function() {
        var restMessage = new sn_ws.RESTMessageV2('`network Access', 'GET network Data By network Name');
        //restMessage.setEndpoint(' https://connectivity-api.netbuz-l.cotp/industrial-networks/networks/');
        restMessage.setHttpMethod('GET');
        var queryParam = gs.getVariable('query_param');
        restMessage.addQueryParameter('parameter_name', queryParam);
        var response = restMessage.execute();
        var responseBody = response.getBody();
        gs.log("check enclave response" + responseBody);
        var httpStatus = response.getStatusCode();
        if (httpStatus == 200) {
            // Store the response body in a variable
            gs.setVariable('response', responseBody);
            return JSON.stringify(responseBody);
            // gs.log("check enclave response"+responseBody);
        }
    },
    type: 'GetEnclaveData'
});

 

 


Thanks & Regards,
Vasanth

View solution in original post

17 REPLIES 17

yes i tried this but in u_comment iam getting "null"

@shabbir9 in script include check if you are getting response or not.

 

BTW send the screen shot of script include code

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

yes iam getting response body in script include....sharing script include iam getting response body in json..i have checked in logs response ...kindly check how to send this responsebody onto that u_comment varibale on catalog item

 

var GetEnclaveData = Class.create();

GetEnclaveData.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getData:function(){
var restMessage = new sn_ws.RESTMessageV2('`network Access', 'GET network Data By network Name');;
//restMessage.setEndpoint(' https://connectivity-api.netbuz-l.cotp/industrial-networks/networks/');
restMessage.setHttpMethod('GET');
var queryParam = gs.getVariable('query_param');
restMessage.addQueryParameter('parameter_name', queryParam);
var response = restMessage.execute();
var responseBody = response.getBody();
gs.log("check enclave response"+responseBody);
var httpStatus = response.getStatusCode();
if (httpStatus == 200) {
// Store the response body in a variable
gs.setVariable('response', responseBody);
// gs.log("check enclave response"+responseBody);
}
},
type: 'GetEnclaveData'
});