3

I am building a Rails (3.0.3) application with some JavaScript using jQuery (1.4.4). On one of the sites I added a click event listener and want to load some content using a AJAX request.

$(document).ready(function() {
  $("a#settings").click(function() {
    $("div#box").load("settings.js");
    return false;
  });
});

When I now open the site "http://localhost:3000/entities/3" where the link with the bound click listener is located and click the link I get a 404 error. This happens because the AJAX request uses the URL "http://localhost:3000/entities/settings.js" and not (as I would exect) "http://localhost:3000/entities/3/settings.js".

medihack
  • 16,045
  • 21
  • 90
  • 134

2 Answers2

1

There is an answer by some of a very similar but library independent question:

This is how relative paths is supposed to work.

protocol://some.domain.name/dir1/dir2/filename

If you specify only a new filename "foo", you get the same protocol, host and dirs, only the file name is changed:

protocol://some.domain.name/dir1/dir2/foo

If you specify a whole path "/dir3/filename2" you get the same protocol and hostname but with another path:

protocol://some.domain.name/dir3/filename2

You can also specify host name "//another.domain.name/dir5/filename3" and get the same protocol but another host, dir and filename:

protocol://another.domain.name/dir5/filename3

What might be confusing is that a webserver internally can add a / at the end of the url if the specified url points to a directory and not to a file.

protocol://some.domain.name/somename

If "somename" is a directory the webserver translates it to

protocol://some.domain.name/somename/

For reference, see step 6 in section 4 of RFC 1808

Community
  • 1
  • 1
medihack
  • 16,045
  • 21
  • 90
  • 134
0

Looking through the jQuery source for $.fn.load and subsequently $.ajax, I can't find anything that tries to account for relative paths. It does extract pieces of information from absolute paths, but that's only to test for remoteness. That being said, try the following and see if the result is any different, because I may have missed something:

$(document).ready(function() {
    $("#settings").click(function() {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "settings.js", true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                $('#box').html(xhr.responseText);
            }
        };
        xhr.send(null);
        return false;
    });
});

My guess is that this may be a browser-specific issue.

Reid
  • 18,959
  • 5
  • 37
  • 37
  • It happens under Chrome and Firefox. I just tried it on different pages (also your script). It always tries to connect one path higher. Eg. localhost/users/kai, then it tries to fetch from localhost/users. The last part of the URL is always deleted. – medihack Jan 21 '11 at 02:00
  • If my script didn't work, then it's a browser problem as far as I know. Can you try going to the page with a trailing slash (`/3/`)? I don't think it would matter, but I'm not the most experienced with this sort of thing. – Reid Jan 21 '11 at 02:03
  • Indeed, it works when the URL has a trailing slash. But it once worked without the trailing slash, too (as far as I remember). What could have happened? It does seem to affect all browsers on my system (Chrome, FF3, Opera). – medihack Jan 21 '11 at 10:05
  • Well, it is probably viewing `3` as a file in the same directory, and so it attempts to resolve `settings.js` as being in the same directory as `3` is. But when you use the trailing slash, it sees `3/` and so it knows that it's a directory, and correctly resolves `settings.js` as being in `3/`. – Reid Jan 21 '11 at 14:42
  • Ok, but what can I do to solve that problem? I don't want to rewrite all my URLs somehow to use a trailing slash. How do normally people use the url in AJAX requests? – medihack Jan 21 '11 at 19:04
  • Could you just output the entire path as seen from system before the script runs? I use that in my CMS to be safe. – plebksig Jan 22 '11 at 15:49