AJAX folder path issue

So. My AJAX request works when I put the php file in the same folder as the html file with the AJAX. Like:

var URL = "question1.php?q1=" + radioValue;

Thing is though that I want to keep question1.php in another folder named "json". I've tried the following:

var URL = "/json/question1.php?q1=" + radioValue;
var URL = "json/question1.php?q1=" + radioValue;

But then the AJAX request skips the path before the folder json for some reason. Like:


..instead of


with "example" being where the ajax.html lies. I simply wanna be able to switch over the website to a server when I feel like it, without changing that path. Suggestions?


Considering your json and example folder are on the level directory level and your ajax is executing from the path /example, you need to do :

var URL = "/../json/question1.php?q1=" + radioValue;  // /../ would navigate to the parent of json and example.

which would internally look like :

source: stackoverflow.com
js interview questions