admin管理员组文章数量:1134247
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.
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.
Share Improve this question edited Jan 13, 2017 at 11:07 030 11.7k15 gold badges86 silver badges127 bronze badges asked Sep 16, 2015 at 9:19 wmashwmash 4,2024 gold badges38 silver badges73 bronze badges 03 Answers
Reset to default 247XMLHttpRequest is a built-in object in web browsers.
It is not distributed with Node. The http module is the built-in tool for making HTTP requests from Node.
Most people making HTTP requests from node use a third party library with a friendlier API. Two popular choices are Axios (for use both in Node.js and browsers) and node-fetch
(which implements the fetch API which is built into browsers and is a modern replacement for XMLHttpRequest.
2022 update: Node 18 has a native implementation of fetch
enabled by default.
If you really want to use XHR in Node.js then there are a couple of third party implementations. xmlhttprequest
(which seems to be unmaintained) and xhr2
(which had an update when I added it to this answer but seems to have been abandoned by the developer since).
Install it with npm,
npm install xhr2
Now you can
require
it in your code.var XMLHttpRequest = require('xhr2'); var xhr = new XMLHttpRequest();
Since the last update of the xmlhttprequest module was around 2 years ago, in some cases it does not work as expected.
So instead, you can use the xhr2 module. In other words:
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:
// 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);
});
With the xhr2 library you can globally overwrite XMLHttpRequest
from 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.
global.XMLHttpRequest = require('xhr2');
本文标签: javascriptXMLHttpRequest module not definedfoundStack Overflow
版权声明:本文标题:javascript - XMLHttpRequest module not definedfound - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736832764a1954770.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论