Javascript 未定义/找到 XMLHttpRequest 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32604460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:56:35  来源:igfitidea点击:

XMLHttpRequest module not defined/found

javascriptnode.jsxmlhttprequest

提问by wmash

This is my code:

这是我的代码:

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.open("GET", "//URL")
xhr.setRequestHeader("Content-Type: application/json", "Authorization: Basic //AuthKey");
xhr.send();

I am getting the error:

我收到错误:

Cannot find module 'xmlhttprequest'

When I remove the first line, I am getting:

当我删除第一行时,我得到:

XMLHttpRequest is not defined

I have searched all over and people have mentioned a problem with Node.js here and there but my installation of Node was correct so I'm not sure what the issue is.

我到处搜索,人们在这里和那里都提到了 Node.js 的问题,但我的 Node 安装是正确的,所以我不确定问题是什么。

回答by Quentin

XMLHttpRequest is a built-in object in web browsers.

XMLHttpRequest 是Web 浏览器中的内置对象。

It is not distributed with Node; you have to install it separately,

它不与 Node 一起分发;你必须单独安装

  1. Install it with npm,

    npm install xmlhttprequest
    
  2. Now you can requireit in your code.

    var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    var xhr = new XMLHttpRequest();
    
  1. 用 npm 安装,

    npm install xmlhttprequest
    
  2. 现在您可以require在您的代码中使用它。

    var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    var xhr = new XMLHttpRequest();
    

That said, the http moduleis the built-in tool for making HTTP requests from Node.

也就是说,http 模块是用于从 Node.js 发出 HTTP 请求的内置工具。

Axiosis a library for making HTTP requests which is available for Node and browsers that is very popular these days.

Axios是一个用于发出 HTTP 请求的库,可用于 Node 和当今非常流行的浏览器。

回答by robe007

Since the last update of the xmlhttprequest modulewas around 2 years ago, in some cases it does not work as expected.

由于xmlhttprequest 模块的上次更新是在大约2 年前,因此在某些情况下它无法按预期工作。

So instead, you can use the xhr2 module. In other words:

因此,您可以使用xhr2 模块。换句话说:

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();

becomes:

变成:

var XMLHttpRequest = require('xhr2');
var xhr = new XMLHttpRequest();

But ... of course, there are more popular modules like Axios, because -for example- uses promises:

但是……当然,还有更流行的模块,比如Axios,因为 - 例如 - 使用承诺:

// Make a request for a user with a given ID
axios.get('/user?ID=12345').then(function (response) {
    console.log(response);
}).catch(function (error) {
    console.log(error);
});

回答by yper

With the xhr2 libraryyou can globally overwrite XMLHttpRequestfrom your JS code. This allows you to use external libraries in node, that were intended to be run from browsers / assume they are run in a browser.

使用xhr2 库,您可以XMLHttpRequest从 JS 代码全局覆盖。这允许您在 node 中使用外部库,这些库旨在从浏览器运行/假设它们在浏览器中运行。

global.XMLHttpRequest = require('xhr2');