![](/img/trans.png)
[英]XMLHttpRequest() send NS_ERROR_FAILURE for localhost with absolute OR relative path
[英]XMLHttpRequest - relative or absolute path?
我正在尝试在本地服务器中进行 HTTP 测试。
在我的 HTML 文件中,我链接src="scripts/scriptedHTTP.js
:
var req = new XMLHttpRequest();
req.open("GET", "file.txt", false);
req.send(null);
console.log(req.responseText);
我在localhost:8000
使用python -m SimpleHTTPServer
。
file.txt
与scriptedHTTP.js
位于同一路径
http://localhost:8000/scriptedHTTP.html
给我Error 404
。
如果我使用node
运行scriptedHTTP.js
,我会得到XMLHttpRequest is not defined
。
我做错了什么?
XMLHttpRequest
将使用页面域(如果您不手动设置)形成请求路径,因此在这种情况下,您只需要指定file.txt的完整路径:
var req = new XMLHttpRequest();
req.open("GET", "scripts/file.txt", false);
req.send(null);
console.log(req.responseText);
请注意, XMLHttpRequest
是 Web 浏览器的一项功能,因此在NodeJS 中不可用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.