简体   繁体   English

open(...) 相对或绝对的 URL

[英]URL of open(…) relative or absolute

I don't quite understand the functioning of the url parameter of XMLHttpRequest open(method, url, async) .我不太明白 XMLHttpRequest open(method, url, async)url参数的功能。 Let's say I have a web server like that:假设我有一个这样的网络服务器:

在此处输入图片说明

page.html sends an asynchronous request to controller.php . page.htmlcontroller.php发送一个异步请求。 As we can only send requests to our own web server, I assume that we don't have to rewrite the website's name in the URL.由于我们只能向我们自己的 Web 服务器发送请求,因此我假设我们不必在 URL 中重写网站名称。

Example: instead of open('GET', 'http://www.mywebsite/controller.php', true) we can simply write open('GET', 'controller.php', true) ).示例:代替open('GET', 'http://www.mywebsite/controller.php', true)我们可以简单地写open('GET', 'controller.php', true) )。

I don't get if this URL is relative to page.html or absolute from the root of the server.我不知道这个 URL 是相对于page.html还是绝对来自服务器的根。


TL;DR: Do I have to write open('GET', 'controller.php?param=1', true) or open('GET', 'folder/controller.php?param=1', true) ? TL;DR:我必须写open('GET', 'controller.php?param=1', true)open('GET', 'folder/controller.php?param=1', true)吗?

与 JavaScript 中的所有 URL 一样,相对 URL 与 JavaScript 代码在其中运行的页面的 URL 相关。

It's relative to the folder of the page.它相对于页面的文件夹。 All URLs in HTML and JavaScript are processed relative to the page (more precisely, the base URL of the page, which can be changed with a <base> tag). HTML 和 JavaScript 中的所有 URL 都相对于页面进行处理(更准确地说,是页面的基本 URL ,可以使用<base>标记更改)。

So you should use:所以你应该使用:

open('GET', 'controller.php?param=1', true);

since the controller is in the same folder as the page calling it.因为控制器与调用它的页面位于同一文件夹中。

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

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