简体   繁体   English

如何获取用于向服务器发出AJAX请求的基本目录

[英]How to get the base directory for making AJAX requests to a server

I'm trying to make a small Node.js Project. 我正在尝试制作一个小的Node.js项目。 The client side of the project will be making many AJAX requests with the server. 项目的客户端将向服务器发出许多AJAX请求。 I'm trying to make code that will work with all web pages, but I discovered that the path is relative to the location of the page. 我正在尝试制作适用于所有网页的代码,但我发现该路径是相对于页面位置的。 How do I make the path start at the base directory of my website? 如何使路径从网站的基本目录开始?

//I've tried doing this
var path = require(path);
var myPath = path.join(__dirname, '../');

/ The error was that I couldn't user require in client side javascript like you can do in Node.js / / 错误是我无法像客户端Node.js中那样在客户端javascript中要求用户 /

The error was: require is not defined. 错误是:未定义require。

Forget about the path module: 忘记path模块:

  • You aren't dealing with directory paths 您没有处理目录路径
  • It isn't available in a web browser 它在网络浏览器中不可用

Use a relative URL with an absolute path. 使用带有绝对路径的相对 URL。

const myPath = "/foo/bar"

Note also that you can't construct a URL that will resolve to somewhere above the webroot. 还要注意,您不能构造将解析为Webroot 之上的URL。 The webroot is the very top. webroot是最顶层的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM