【问题标题】:Type Error: XMLHttp is not a constructor类型错误:XMLHttp 不是构造函数
【发布时间】:2018-08-18 11:08:07
【问题描述】:

我已经在 J​​avaScript 中尝试了如下代码:

var XMLHttpRequest = require("xmlhttprequest");

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.codecademy.com/",true);
xhr.send();

console.log(xhr.status);
console.log(xhr.statusText);

当我在命令提示符“node ***.js”中使用 node.js 运行此代码时,会出现一条错误消息,指出“TypeError:XMLHttpRequest 不是构造函数”。感谢您对为什么会出现此错误的任何帮助。谢谢。

【问题讨论】:

  • 一定是因为第一行失败了。
  • node.js 没有直接的 Ajax-API。您必须使用第三方模块或来自node.jsnet 组件

标签: javascript node.js xmlhttprequest


【解决方案1】:

我认为您需要将模块加载为smlhttprequestdemo

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

【讨论】:

  • 感谢您的回答,绝对有帮助。
【解决方案2】:

第一行应该是:

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

请参阅documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2019-07-25
    • 2013-09-26
    • 2023-02-16
    • 2019-09-01
    • 2020-02-18
    • 1970-01-01
    相关资源
    最近更新 更多