Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative vs. absolute urls in jQuery AJAX requests

Tags:

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

like image 683
fresskoma Avatar asked Mar 02 '10 13:03

fresskoma


People also ask

Should I use absolute or relative URLs?

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.

What is the difference between relative URL and absolute URL?

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.

Which is an example of an absolute URL?

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).

What are relative URLs?

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.


1 Answers

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.

like image 105
Sampson Avatar answered Nov 03 '22 19:11

Sampson