[英]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.