Sylnois
Sylnois

Reputation: 1631

jQuery Ajax call with multiple URLs

I want to run a Ajax call, but I am having issue locating the script.php file because of my htaccess rewrite rules. I have a index.php, script.php and .htaccess in the same folder. In the htaccess, there is a RewriteRule which formats a URL like this "domain.com/index.php?bla=123" into this "domain.com/123".

However, my javascript cannot locate the PHP-Script since the path is set to "script.php" when the URL becomes "domain.com/asdf/". Any idea how to run the script from the js whether through the root or a FAKE directory.

url: "script.php" || "../script.php",

Upvotes: 3

Views: 2341

Answers (2)

Josh Mein
Josh Mein

Reputation: 28625

Based on the further explanation of your situation, you should just be able to use "/script.php" as the path for your ajax call. In javascript, starting a path with / takes you to the root of the server; therefore, you can just progress from there and find your script files no matter where the current page is located.

If for some reason this does not work, you can use window.location.hostname to get the path to the server like so:

var scriptUrl = window.location.hostname + "/script.php";

Edit:

Based on your comments, your path would have to be your /something/script.php instead of just /script.php for both possible solutions.

Upvotes: 0

Alon Eitan
Alon Eitan

Reputation: 12025

you can use inline condition, like this example:

url: (4 > 2 ? "script.php" : "../script.php"),

in this example you will always end up with the url "script.php", so change the 4 > 2 condition to your own needs

Upvotes: 1

Related Questions