mgrenier
mgrenier

Reputation: 1447

Simple Explanation of AJAX

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.

Upvotes: 0

Views: 540

Answers (3)

JDB
JDB

Reputation: 25820

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.

Upvotes: 2

Gary Storey
Gary Storey

Reputation: 1814

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.

Upvotes: 1

Mike H.
Mike H.

Reputation: 1751

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.

Upvotes: 1

Related Questions