"XMLHttpRequest" is not defined

manishk1
Kilo Contributor

Hello Community members,

I have a requirement to get   some external data in include script(server side).   I am   using the following code for the same but i   am getting error like "XMLHttpRequest" is not defined .

var client=new XMLHttpRequest();

  client.open("GET","https://google.co.in");

  client.setRequestHeader('Accept','application/json');

  client.onreadystatechange = function(){

  if(this.readyState == this.DONE){

  document.getElementById("response").innerHTML=this.status + this.response;

  }};

  client.send();

I   tried the following code as well but in this case i   am   getting different error   like.

Uncaught ReferenceError: Packages is not defined(…)

manish.html:13 Uncaught ReferenceError: Packages is not defined(…)

  var httpclient = Packages.org.apache.commons.httpclient;    

  var HttpClient = httpclient.HttpClient;    

  var GetMethod = httpclient.methods.GetMethod;    

  var client = new HttpClient();      

  var get = new GetMethod('https://randomuser.me/api/');      

  var status = client.executeMethod(get);      

    var result = get.getResponseBodyAsString();      

  return status+result;

Please help.

4 REPLIES 4

Melghi
Kilo Expert

Hi,



try this script and edit it based on you need:



var request = require('request');  


var username = 'admin';  


var password = 'admin';  


var instance = 'demo003';  


var table = <table_name>;  


var query = 'active=true';  


var url = "https://google.co.in"  


request.get(url, {  


  'auth': {  


      'user': username,  


      'pass': password,  


      'sendImmediately': false  


  }  


}).on('response', function(response){  


      console.log(response.statusCode);  


      console.log(response.headers['content-type'])  


      response.on('data', function(data) {  


              console.log('data: ' + data);  


      })  


});  



  1. var request = require('request');  
  2. var username = 'admin';  
  3. var password = 'admin';  
  4. var instance = 'demo003';  
  5. var table = 'task';  
  6. var query = 'active=true';  
  7. var url = "https://" + instance + ".service-now.com/" + table + "_list.do?sysparm_query=" + query + "&JSONv2"  
  8. request.get(url, {  
  9.   'auth': {  
  10.       'user': username,  
  11.       'pass': password,  
  12.       'sendImmediately': false  
  13.   }  
  14. }).on('response', function(response){  
  15.       console.log(response.statusCode);  
  16.       console.log(response.headers['content-type'])  
  17.       response.on('data', function(data) {  
  18.               console.log('data: ' + data);  
  19.       })  
  20. });  

How to call a service now JSON api from a different system ?


i encountered the same issue,



function snTFSGetXhr(){


if (typeof XMLHttpRequest == 'undefined') {


                      gs.addInfoMessage("undefined.");


              }


else{


gs.addInfoMessage("ok");


}


}



XMLHttpRequest is undefined


tried npm request as per Mohamed's reply, still doesn't work



i'm using standard practice instance, and tried script in both Script Include and Business Rule.



wonder why it shows XMLHttpReuqest not defined


Community Alums
Not applicable

XMLHttpRequest won't run on server side, since this method is used to execute AJAX requests from the client side to server.

Hence you should write that on Client script or any script field that is meant to work on client side.

 

Thanks,

kush bajpai

Oleg
Mega Sage

One should use sn_ws.RESTMessageV2 instead of XMLHttpRequest if you need to make Ajax request on the server side. Your code could be rewritten as about the following:

try {
    var restMessage = new sn_ws.RESTMessageV2();
    restMessage.setHttpMethod("GET");
    restMessage.setEndpoint("https://google.co.in");
    restMessage.setRequestHeader("Accept", "application/json");
    restMessage.setRequestHeader("Content-Type", "application/json");
    restMessage.setRequestBody(JSON.stringify({short_description: "Test incident"}));
    var response = restMessage.execute();
    if (response.getStatusCode() === 200) {
        var responseBody = response.getBody();
        var responeData = JSON.parse(responseBody);
        // process successful response
    } else if (response.getStatusCode() === 0) {
        // process connect timed out
    } else {
        // process error response
        var statusCode = response.getStatusCode();
        var erorMessage = response.getErrorMessage();
        var contentType = response.getHeader("Content-Type");
        var body = response.getBody();
        // ...
    }
} catch (ex) {
    // process exception
}

If you need to communicate with some internal server of your company, then you could need to use MID server as proxy. See the corresponding example here. By searching you will find many other interesting examples of usage RESTMessageV2, like this one for example.