-1

I would like to programmatically access to specific URL content (MyURL/more_videos).

While using the Web Inspector from Safari, I noticed the data I want is in an XHRs folder. After googling about XHR, I tried some code with node.js but without any success.

Here is what I found at https://stackoverflow.com/a/32860099/8726869 :

function readBody(xhr) {
    var data;
    if (!xhr.responseType || xhr.responseType === "text") {
        data = xhr.responseText;
    } else if (xhr.responseType === "document") {
        data = xhr.responseXML;
    } else {
        data = xhr.response;
    }
    return data;
}

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        console.log(readBody(xhr));
    }
}
xhr.open('GET', ‘MyURL/more_videos', true);
xhr.send(null);

Nevertheless, I didn't get anything the console neither error nor response.

I have attached some screen shots of the web inspector:

Screenshot web inspector 1

Screenshot web inspector 2

Pm22
  • 21
  • 6

1 Answers1

0

XMLHttpRequest is used in the browser to get a URL. In node.js, you should use the node.js http library (docs). Here's a simple example:

var http = require('http');

http.get({
    host: 'httpbin.org',
    path: '/get'
}, response => {
    let body = "";
    response.on('data', d => {
        body += d;
    });
    response.on('end', function() {
        console.log(body);
    });
});
Matthias
  • 648
  • 6
  • 18