XMLHttpRequest模块未定义/找到 [英] XMLHttpRequest module not defined/found
本文介绍了XMLHttpRequest模块未定义/找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.open("GET", "//URL")
xhr.setRequestHeader("Content-Type: application/json", "Authorization: Basic //AuthKey");
xhr.send();
我收到错误:
Cannot find module 'xmlhttprequest'
当我删除第一行时,我得到:
When I remove the first line, I am getting:
XMLHttpRequest is not defined
我已经搜遍了所有人,并且人们已经提到了Node.js的问题,但我的Node安装是正确的,所以我不确定是什么问题。
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.
推荐答案
XMLHttpRequest是网络浏览器中的内置对象。
XMLHttpRequest is a built-in object in web browsers.
它不与Node一起分发;你必须单独安装,
It is not distributed with Node; you have to install it separately,
-
用npm安装,
Install it with npm,
npm install xmlhttprequest
现在您可以在代码中 require
。
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
也就是说,Node附带 http模块,这是从Node发出HTTP请求的常用工具。
That said, Node comes with the http module which is the normal tool for choice for making HTTP requests from Node.
这篇关于XMLHttpRequest模块未定义/找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文