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:

http://localhost/json/question1.php?q1=Ok

..instead of

http://localhost/example/json/question1.php?q1=Ok

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?

Answer

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 :

http://localhost/example/../json/question1.php?q1=Ok
source: stackoverflow.com
js interview questions