Ad

XMLHttpRequest Onload Method Not Working At Node Js Back End Side

- 1 answer

I Try to load url on node Js at back end side.But It not working

I also installed xmlhttprequest in node js. using "npm i xmlhttprequest"

global.XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var request = new XMLHttpRequest();
request.responseType = 'json';
request.open('GET', 'https://api.github.com/users/hadley/orgs');
request.onload = function() {
  console.log(request.response);
  //console.log(request.response.byteLength);
};
request.send();

I am getting undefind result in console. But if I will try to run This code in browser it running fine.

Ad

Answer

You should just replace "request.response" with request.responseText

global.XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var request = new XMLHttpRequest();
request.responseType = 'json';
request.open('GET', 'https://api.github.com/users/hadley/orgs');
request.onload = function() {
  console.log(request.responseText);
  //console.log(request.response.byteLength);
};
request.send();
Ad
source: stackoverflow.com
Ad