Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax and relative urls

I really don't get this. I have the following 'get' request:

$.ajax( {
    url: "api/getdirectories/",
    dataType: 'json',
    success: function ( data ) {
        // Do stuff
    }
} );

This is in a page served up to me by my staging server, http://atlas/Reporter. On my local dev box, this works, and in looking at fiddler I see the correct URL http://localhost/Reporter/api/getdirectories. On the staging server however the request is to http://atlas/api/getdirectories, which is invalid and I get a 404 error.

Why is the 'Reporter' part of my path being dropped on the staging server? In looking at the document, the URL, baseURI, documentURI are all http://atlas/Reporter, and the domain is atlas. Exactly the same as on my local dev box (except it is all 'localhost' instead of 'atlas'.

This problem has plagued me for a while now. I think I have it figured out and all is good, and then I run into it again. I don't think it is a same origin policy issue, as I am requesting the data from the same site the page originated from.

So, how exactly is the full url determined for the request? It doesn't seem to be one of the document variables mentioned above being concatenated onto my relative URL... so how does this work?

Edit: Removed the '/' from the URL as it is distracting from the real issue- behavior is the same with or without it.

like image 793
Nicros Avatar asked Mar 28 '13 17:03

Nicros


People also ask

What are Ajax URLs?

Ajax URL is one of the core functionality that is provided by ajax to the user, in which users can use this method as per their requirement and perform different operations. Ajax URL uses the asynchronous HTTP request to send the request over the server, as well as we can use URL for retrieving data from the server.

Can we send two URLs in Ajax?

You can't call 2 url simultaneously. but you can call one after another. You can't put two URLs in an ajax call.

Does Ajax need a URL?

You need to specify the url because whenever you make a server request (whether it be using AJAX, or synchronous-old fashion way) you need to tell the browser who to send the request to. Almost all the examples I saw in the jQuery documentation page have a specified URL or some sort (url: "test.

What is an example of a relative URL?

Rather than including the entire URL for each page you link, relative URLs cut down on the workload and time needed. For example, coding /about/ is much faster than https://www.example.com/about .


1 Answers

Not sure if this is related, but when jQuery ajax builds relative urls, it matters if the current page url ends with "/" or not


If you invoke a page without an ending /:

http://atlas/Reporter  

the relative urls from ajax calls on that page are ignoring the last segment:

http://atlas/_relative_url_passed_to_ajax

If you end with /:

http://atlas/Reporter/  

the relative urls are using the last path segment Reporter:

http://atlas/Reporter/_relative_url_passed_to_ajax
like image 115
Florin D Avatar answered Sep 20 '22 13:09

Florin D