Skip to content

URL of open(…) relative or absolute

I don’t quite understand the functioning of the url parameter of XMLHttpRequest open(method, url, async). Let’s say I have a web server like that:

Enter image description here

page.html sends an asynchronous request to controller.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.

Example: instead of open('GET', 'http://www.mywebsite/controller.php', true) we can simply write 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.

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


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).

So you should use:

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

since the controller is in the same folder as the page calling it.