Where is the problem?

// 1. Создаём новый объект XMLHttpRequest var xhr = new XMLHttpRequest(); // 2. Конфигурируем его: GET-запрос на URL 'phones.json' xhr.open('GET', 'https://api.privatbank.ua/p24api/pubinfo?json&exchange&coursid=5', false); // 3. Отсылаем запрос xhr.send(); // 4. Если код ответа сервера не 200, то это ошибка if (xhr.status != 200) { // обработать ошибку console.log( xhr.status + ': ' + xhr.statusText ); // пример вывода: 404: Not Found } else { // вывести результат console.log( xhr.responseText ); // responseText -- текст ответа. } 

    1 answer 1

    XMLHttpRequest not supplied by default in Node.js. It must be installed using npm . Based on this issue .

    When using it is necessary not to forget about require :

     var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var xhr = new XMLHttpRequest(); 
    • I have it installed. Version 3.10.3. Can it somehow need to connect? - Vania Kycher
    • @VaniaKycher judging by the plugin page , the latest version of the plugin is 1.8.0. Where did you get 3.10.3? - Regent
    • one
      @VaniaKycher added an example of its use to the answer: you may have missed the first line. - Regent
    • Hah .. now require is not defined. The require module is installed - version 2.4.20 - Vania Kycher
    • @VaniaKycher You're running this script on the server, and not in the browser? - Regent