XMLHttpRequest - 相对或绝对路径? [英] XMLHttpRequest - relative or absolute path?

查看:323
本文介绍了XMLHttpRequest - 相对或绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在本地服务器上进行 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 with python -m SimpleHTTPServer



file.txt scriptedHTTP.js



http:// localhost:8000 / scriptedHTTP.html 得到我错误404



如果我用 node 运行 scriptedHTTP.js ,我得到 XMLHttpRequest is没有定义



我做错了什么?



..

解决方案

XMLHttpRequest 将使用页面域构成请求路径(如果y因此在这种情况下,您只需指定 file.txt 的完整路径:

  var req = new XMLHttpRequest(); 
req.open(GET,scripts / file.txt,false);
req.send(null);
console.log(req.responseText);

请注意, XMLHttpRequest 是web因此浏览器在 NodeJS
中不可用

I'm trying to make HTTP tests in my local server.

On my html file, I link src="scripts/scriptedHTTP.js:

   var req = new XMLHttpRequest();
   req.open("GET", "file.txt", false);
   req.send(null);
   console.log(req.responseText);

I serve on localhost:8000 with python -m SimpleHTTPServer.

file.txt is on the same path as scriptedHTTP.js

http://localhost:8000/scriptedHTTP.html gets me Error 404.

if I run scriptedHTTP.js with node, I get XMLHttpRequest is not defined.

what I am doing wrong?

any help much appreciated..

解决方案

XMLHttpRequest will form the request path using the page domain (if you don't set it manually), so in this case you just need to specify the complete path of your file.txt:

var req = new XMLHttpRequest();
req.open("GET", "scripts/file.txt", false);
req.send(null);
console.log(req.responseText);

Note that XMLHttpRequest is a feature of web browsers so is not available in NodeJS

这篇关于XMLHttpRequest - 相对或绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆