JQuery Ajax call with multiple urls

I want to trigger an Ajax call, but I have a problem finding my script.php file due to my htaccess rewrite rules. I have index.php, script.php and .htaccess in the same folder. There is a RewriteRule in htaccess that formats a url like this "domain.com/index.php?bla=123" into this "domain.com/123".

However my javascript cannot find PHP-Script as the path is set to "script.php" when the url becomes "domain.com/asdf/". Any idea how to run a script from js via root or FAKE directory.

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

      

+3


source to share


2 answers


Based on the following explanation of your situation, you should just use "/script.php"

as the path for your ajax call. In javascript, running a path with /

will take you to the server root; so you can just go from there and find your script files no matter where the current page is.

If for some reason that doesn't work, you can use window.location.hostname

to get the path to the server like this:

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

      



Edit:

Based on your comments, your path should be yours /something/script.php

, not just /script.php

for both possible solutions.

0


source


you can use a built-in condition like this example:

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

      



in this example you will always get the url "script.php", so change the condition 4 > 2

to your own needs

+1


source







All Articles