I'm having a hard time getting my AJAX requests to work on a staging server. It all worked fine on my development machine, but as soon as I uploaded it, all my AJAX requests stopped working. I found out that, if I change the relative urls (eg. "index.php") to absolute urls ("http://example.com/index.php") the requests work again, but I do not understand why.
Example request:
jQuery.post('index.php', {id: 1234, action: 1, step: 1}, function(data) { /* something */ });
This does not work, I does not even show up in the firebug console. The success handler is called though, which is very confusing.
This works just fine:
jQuery.post('http://example.com/index.php', {id: 1234, action: 1, step: 1}, function(data) { /* something */ });
Can anybody explain why AJAX requests behave in this way? x_X
An absolute URL contains more information than a relative URL does. Relative URLs are more convenient because they are shorter and often more portable. However, you can use them only to reference links on the same server as the page that contains them.
An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point. In effect, the "complete URL" of the target is specified by concatenating the absolute and relative URLs.
An absolute URL is the full URL, including protocol ( http / https ), the optional subdomain (e.g. www ), domain ( example.com ), and path (which includes the directory and slug).
For example, https://cart.com is an absolute URL. Relative URLs. A relative URL typically contains only the path to a specific file. In context to the Cart.com online stores system, these typically begin with a forward slash. The forward slash tells the browser to go to the domain of the site and look for a file.
Try adding a /
before index.php
in your first example to force it to look from root. Double check to make sure your directory-structures are exactly the same with regards to where index.php
is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With