为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 Node.js 读取外部文本文件?

如何使用 Node.js 读取外部文本文件?

宝慕林4294392 2021-10-21 15:07:21
所以我正在尝试读取外部(如来自不同域的)文本文件,但我尝试过的任何内容都不起作用。通常发生的是响应是undefined,或者根本没有。这是我尝试过的一件事:// package xmlhttprequest is installed// this was copy-pasted from some other stackoverflow questionvar XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;var txtFile = new XMLHttpRequest();txtFile.open("GET", "https://snappeathing282346239557829348747259837723489.000webhostapp.com/pin.txt", true);txtFile.onreadystatechange = function() {  if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.    if (txtFile.status === 200) {  // Makes sure it's found the file.      allText = txtFile.responseText;       lines = txtFile.responseText.split("\n"); // Will separate each line into an array    }  }}console.log(txtFile.send(null)) // Logs response to console我得到的回应是undefined,我怎样才能得到 123(123 是 txt 文件包含的内容)而不是记录undefined?
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 186 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信