0

I have a questions that anyone with a little ajax knowledge should be able to answer fairly easily, I just haven't found the explanation that I am looking for online anywhere so I thought I would ask here. I am working on an existing site and I don't have any previous ajax knowledge.

There is a JQuery ajax call be made in this fashion:

$.ajax({
    url:"/example/url/?x=1&y=2",
    type:"GET",
    dataType:'JSON',
    success: function (data) {
         //some code
    }
});

My confusion lies in the url property. In my case "/example/url/" does not refer to any directory or page in the website. I understand that x and y are parameters and the site is working as it should, however I don't understand exactly what goes on when I click the button that executes this script. I know it is grabbing data from the SQL Server DB for this site I just don't know how the query is being formulated. If anyone can example exactly what happens when this ajax call is run I would greatly appreciate it.

4
  • 2
    It goes take the file index.php in the folder yoursite.com/example/url. If such folder doesn't exist, you may have an .htaccess rewriting the URL. Commented Sep 2, 2014 at 20:15
  • 2
    How can you possibly guess that the application is using php?
    – Mike H.
    Commented Sep 2, 2014 at 20:21
  • @MikeHometchko To be honest, I don't even know how asp.net work or anything, but doesn't every web application need some kind of rewrite to work if the current URL is not a file? Even if the rewrite is a controller or a function? How do the server know that you are requesting a function if there is no rewrite on the server? (Serious question, I'm interested in learning) Commented Sep 2, 2014 at 20:42
  • 3
    @Karl-AndréGagnon yes that's exactly right. IIS takes care of the URL routing and passes the request to a function/method in the case of MVC. This is a high-level question and warrants a high-level response, though.
    – Mike H.
    Commented Sep 2, 2014 at 20:52

3 Answers 3

2

First, you have to understand that paths are completely arbitrary on a computer. They are a convenience, nothing more. An extended name that's easier for humans to remember. Basically, "there is no spoon".

We like to put things in "file cabinets" because that's how things exist in the "real" world. If you put something in the top drawer, it cannot be simultaneously located in the bottom drawer. So we tend to organize things around the concepts of "folders". Paths are an extension of this concept - folders within folders.

enter image description here

To the server, a path is nothing more than an array of bytes. Most servers use this path as an extended name which obeys the rules of the OS's file system, but it doesn't have to. Indeed, Microsoft's MVC platform doesn't use paths as references to files at all. Instead, paths are used to group commands, more like a menu system with menus and submenus.

enter image description here

//MyDomain.com/File/New  
//MyDomain.com/File/Open  
//MyDomain.com/File/Save  
//MyDomain.com/File/SaveAs  

//MyDomain.com/Edit/Copy  
//MyDomain.com/Edit/Cut  
//MyDomain.com/Edit/Paste  

So when you ask "what does this URL point to?" the answer is completely dependent on the server and how the server chooses to interpret that URL (which, ultimately, was a decision made by the people who programmed the server). Some servers obey they file system to a certain degree, others use the URL as a way of organizing commands, while still others use a mix of the two. I'm sure there are other ways of using the URL, but I think you get the point.

Using server-side code, it is often possible to override the default behavior of the server. In ASP.NET, for example, it's possible to intercept requests and manually process them. So while the default behavior for ASP.NET is to go fetch a file within a particular folder, you could actually implement behavior similar to MVC. In MVC, it's possible to do the reverse - have the server serve up files based on a relative URL path. So knowing the default behavior of the server still won't fully answer the question since the website developer may have changed that behavior for one reason or another.

2
  • Thank you for your answer, this helps me get a better understanding. However I am still confused. MVC is being used in this case, but I still don't know how it knows what data to fill the "data" object with. "X" and "Y" seem to be SQL like parameters for a query to an SQL database but I don't see where it is getting the rest of the query from. This happens in several locations and all the values of the URLs in the AJAX calls begin with "/example/...". What I do know is that the server is definitely not handling it like a convention path, because that path does not exist.
    – mgrenier
    Commented Sep 3, 2014 at 12:11
  • Another comment helped pick up where you left off, thanks for the help!! Greatly appreciated.
    – mgrenier
    Commented Sep 3, 2014 at 12:20
1

The answer is: it totally depends on your application.

If the application is ASP.NET framework like, say, MVC....the URL follows the pattern {Controller}/{Action} so you'd need to look in the ExampleController for the ActionResult/JsonResult method called URL. If your backend is running on PHP, follow the instructions from the comment under your question.

Invariably the ajax call is looking for either a page, method or something that follows the path you see there. What the actual path is and where to look can differ.

1
  • It does use MVC framework. Simple as this answer is I think it helps me a lot. There is a ControllersApi and I have found some functions within there which do create SQL queries and return results in accordance with what I was expecting. Thanks for you help!
    – mgrenier
    Commented Sep 3, 2014 at 12:19
1

Expanding on what @mike-hometchko said above, it really depends. Without going into a lot of technical details :

IIS uses default.htm , default.aspx, etc as the default page name.

Apache is index.htm, index.php, etc as the default.

In nodejs, you set up your own url (called routes) and, when the url is requested, you control what gets sent back as a response.

If you are hitting a REST API, there isn't really a page it's (in simple terms) a web app monitoring different URLs for data to be sent and responds accordingly.

1
  • In my case its an IIS site using MVC if that helps.
    – mgrenier
    Commented Sep 3, 2014 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.