1. Establish a connection
The most common way that web servers and clients interact is through a connection that follows a Transmission Control Protocol or TCP . In principle, when using TCP, a connection is established between client and server machines through a series of back-and-forth checks. Once the connection is established and open, data can be sent between the client and server. This connection can also be called Session .
There is also UDP or the User Datagram Protocol , which has a slightly different way of communicating and comes with its own set of pros and cons. I believe that recently some browsers may have started using a combination of the two to get the best results.
There is still a ton to say, but if you are not going to write a browser (or become a hacker), this should not depend too much on you.
2. Sending packages
Once the client-server connection is established, data packets can be sent between them. TCP packets contain various bits of information that aid in the exchange of data between two ports. For web programmers, the most important part of the package will be the section that includes the HTTP request .
HTTP , Hypertext Transfer Protocol is another protocol that describes what the makeup / format of these client-server messages should look like.
The simplest example of the corresponding part of the packet sent by the client to the server is as follows:
GET /index.html HTTP/1.1 Host: www.example.com
The first line here is called the Response line . GET describes the method to be used (others include POST, HEAD, PUT, DELETE, etc.) /index.html describes the requested resource. Finally, HTTP/1.11 describes the protocol used.
The second line in this case is the only header field in the request, in which case it is the HOST field, which is a kind of alias for the server IP address given by DNS.
[Since you mentioned this, the difference between a GET request and a POST request is simply that the parameters in the GET request (for example: form data) are included as part of the Response line , while the parameters in the POST request will be included as part of the Message Body (see below).]
3. Receive packets
Depending on the request sent to the server, the server will scratch its head, think about what you asked, and respond accordingly (otherwise, how do you program it).
Here is an example of sending a response packet from the server:
HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 ... <html> <head> <title>A response from a server</title> </head> <body> <h1>Hello World!</h1> </body> </html>
The first line here is the Status Line , which includes a numerical code along with a brief text description. 200 OK obviously means success. For example, most people are also familiar with 404 Not Found .
The second line is the first of the Response Header Fields . Other fields added include date , Content-Length and other useful metadata.
Below the headers and the desired blank line, finally, the (optionally) Message Body . Of course, this is usually the most exciting part of the answer, as it will contain things like HTML for our browsers, to display JSON data for us, or almost everything you can encode in the return .
4. AJAX, asynchronous JavaScript and XML
Based on all this, AJAX is easy enough to understand. In fact, packets sent and received may look identical to non-ajax requests.
The only difference is how and when the browser decides to send the request packet. Typically, when a page is refreshed, the browser sends a request to the server. However, when issuing an AJAX request, the programmer simply tells the browser to send the packet to the NOW server, and not to refresh the page.
However, given the nature of AJAX requests, typically a Message Body will not contain the entire HTML document, but will request smaller, more specific bits of data, such as a request from a database.
Then your JavaScript, which calls Ajax, can also act based on the response. Any JavaScript method is available, since an Ajax call is another JavaScript function. This way you can do something like innerHTML to add / replace content on your page with the HTML returned by the Ajax call. Alternatively, you can also do something like calling Ajax, which just needs to return True or False, and then you can call some JavaScript function with an if else statement. As you can hope, Ajax has nothing to do with HTML for a single word, it's just a JavaScript function that makes a request from the server and returns a response, whatever it may be.
5. Cookies
The HTTP protocol is an example of an idle protocol . This basically means that each pair of Request and Response (as we described) is processed independently of other requests and responses. Thus, the server does not need to track all the thousands of users who currently require attention. Instead, it can simply respond to each request individually.
However, sometimes we want the server to remember us. How would it be unpleasant if every time I inspected my Gmail I had to register again because the server forgot about me?
To solve this problem, the server can send cookies , which will be stored on the client machine. The server can send a response that tells the client that the cookie has been saved and what it should contain. The client browser is responsible for storing these cookies on the client system, so the location of these cookies will depend on your browser and OS. It is important to understand that these are just small files stored on the client machine that are actually readable and writable by anyone who knows how to find and understand them. As you can imagine, this poses several different potential security threats. , cookie, . ( cookie, , , , , , cookie.
, , Cookie Request Header Fields , : ", , , , " " , , ", .
6.
HTML5 Cookies. cookie, . .
, .
7.
, . -, Google.