"XMLHttpRequest" is not defined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2017 12:40 AM
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.
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2017 02:51 AM
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);
})
});
- var request = require('request');
- var username = 'admin';
- var password = 'admin';
- var instance = 'demo003';
- var table = 'task';
- var query = 'active=true';
- var url = "https://" + instance + ".service-now.com/" + table + "_list.do?sysparm_query=" + query + "&JSONv2"
- 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);
- })
- });
How to call a service now JSON api from a different system ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2017 10:40 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2019 04:25 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2019 05:14 AM
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.