What exactly happens when we use ajax

I don’t understand what exactly happens when we use ajax.Page is not updated every time, which means what? this is due to the page_load method or the fact that

+4
source share
6 answers

The browser makes http calls in the background thread, and you use javascript to change the DOM when returning the result. This does not allow you to refresh the entire page and may optionally change the details on it.

See the wikipedia section for more information.

If you do ajax in an asp.net application, the page_load page is called for the page you are requesting, like a regular call. This is a rendering in a browser that is different. ¨ Key concepts: background calls and scenarios.

+5
source

To explain AJAX, you need to understand the non-AJAX method and see the difference, so I will give the same example in a regular mail method and AJAX path.

In the script there is a list of phone numbers into which you want to add any number of phone numbers for the client, each entry has two fields, type and number. Type is a description, for example. Mobile homework.

Traditional

The user sees a list of phone numbers, the last of which is empty and editable. They want to add another phone number to fill in the empty fields and click the "Add" button. The form is submitted, so the data is transferred to the http message and sent to the server, the server code adds the phone record to the database and starts constructing html for the new page, so it starts with htm, head, includes the whole script, and then the data table, including a new line with a new phone number and another empty field.

Inquiry

POST /addPhoneNumberForm Http 1.1 HOST:www.host.com USER-AGENT:... ... newPhoneType=Home&newPhoneNumber=00649123456 

answer

 <DOCTYPE ....> <html> <head> <title="..."> <link type="text/css" href="standard.css" /> <script type="text/javascript" src="jQuery1.4.js"></script> ... </head> <body> <div id="headerAndNav"> .... </div> <div id="content"> .... <table id="addPhoneRecords" class="dataentry"> <tr><th>Description</th><th>Number</th></tr> <tr><td>Mobile</td><td>0044987654331</td></tr> <tr><td>Work</td><td>0044987654332</td></tr> <tr><td>Holiday</td><td>0044987654333</td></tr> <!-- Newly Added --> <tr><td>Home</td><td>00649123456</td></tr> <tr><td><input type=text .../></td><td><input type=text .../></td></tr> </table> .... </div> <div id="footer"> ... </div> </body> </html> 

As soon as the user’s browser receives this answer, it discards everything that the user sees, replaces it with a white, blank screen. And start the html parsing obtained as a brand new page. Any image files / Css / Javascript should be re-rendered (or at least check if the cache is valid). Any javascript files should be fired, and all accompanying DOM events should be recorded (document.onLoad, etc.).

So what happened, the user sent the data, and the server responded with a completely new page. So much time was spent on network activity, time and processor cycles both on the server and on the client, since they had to generate and analyze a full html page.

AJAX Way

The user sees a list of phone numbers, the last of which is empty and editable. They want to add another phone number to fill in the empty fields and click the "Add" button. This beat was the same as the traditional one, now it becomes different. When the user clicks the button instead of the form sending the javascript piece, collects the information received from the form, and makes an AJAX request to the server using XMLHttpRequest, it sends a request that looks like a form message, but the result will be returned as a javascript string not like a web page.

Inquiry

 POST /addPhoneNumberAJAX Http 1.1 HOST:www.host.com USER-AGENT:... ... newPhoneType=Home&newPhoneNumber=00649123456&ajax=true&type=json 

There are a large number of options that the server can return, I chose one example answer

 {phoneType:"home",phoneNumber:"00649123456"}; 

This is in JavaScript Object Notation (JSON) and represents an object with two types phoneType and numberNumber. Once this is received, JavaScript is used to dynamically add a new row to an existing table. Javascript will look something like this.

 addPhoneViaAjax(type, number, reqObj, xmlhttp){ // Construct url and declare parameters for our request xmlhttp.open("POST","addPhoneNumberAJAX?newPhoneType="+type ...,false); // Send the request and wait for the response xmlhttp.send(null); // For example assume response successful // Get the response as text var responseStr = xmlhttp.responseText; // Convert the response to object, preferably using one of the JSON parsing libaries var responseObj = eval(responseStr); // Please use one of the JSON parsing libaries look at Jquery.forms plugin // Use jquery javascript libary to update the table in place. // by finding the last row of the table and instering html before it. jQuery("table#addPhoneRecords tr:last-child").before( "<tr><td>" + responseObj.phoneType + "</td><td>" + responseObj.phoneNumber + "</td></tr>"); } 

Summary Thus, using the traditional method, the browser considers the answer to the whole new page and must do everything necessary to draw a new page. Using the ajax approach, the browser stays on one page and you use javascript to change one very small fragment of the page.

+4
source

Ajax is simple Javascript code. and the page does not refresh by itself, you need to refresh it - if you consider it necessary (we usually base it on your server after the request has returned).

Yes, it opens a background connection from your browser to the ASP page. and when you are done - you can only update one DOM object on your page.

What is this for? Why are we using this? look at facebook, for example, when you reject a notification, ignore, approve, comment on your friend’s opinion - you do not refresh the page - you (as a developer) can change the properties of the client (in the database for ex) from / out, letting him know.

plus, you can do all of the above by simply refreshing the page (which is very annoying for the client), and you always need to protect all the arguments that need to be passed to each new document.

The ajax method allows you to do one action without moving anywhere. it is very convenient as a developer to use it, and the client enjoyed it more.

and there are JS frameworks that make it very simple. for example: (this example is a jQuery example ..)

 function call_my_page() { $.post("/get_list.asp", { param1: "a", param2: "b" }, function ( server_response ) { alert(server_response); if ( server_response == 1 ) { window.location.refresh; } } } call_my_page(); 

get_list.php is the file on your server that will be published ($ .post)

param1, param2 - you go to your server.

and if the server page file returns the answer "1", then - your page will be updated. (or only DIV / SPAN / TABLE will be replaced with new HTML)

JQuery is the best.

+2
source

Using JavaScript, you can make calls to web pages. In addition, using JavaScript, you can replace the contents of the page with something new (by manipulating the DOM ).

So, Ajax (asynchronous JavaScript) is the process of combining these two methods. You visit a page (usually one on your own server), get its results, and then take this data and do something with it (sometimes change the page).

This is the main idea.

+1
source

Go to any SO post with more than a few comments and you will see a link that says something like another 99 comments below. Now, if you click on it, you will see that the specified 99 comments are loaded in a second or two, and the remaining page remains unchanged. This is because the page is not updating. This would require a longer page refresh as this would mean reloading the entire html content.

Instead of re-sending the entire html page (including other messages in the same stream), the server sends only the requested data (only these 99 comments in our example).

The possibilities are huge. The server can return any data - html or text or xml or json or whatever. You can display the received data using javascript.

Server-side code handles an AJAX request in the same way that it handles a regular HTTP request (you can override this using the HTTP_X_REQUESTED_WITH header, though).

+1
source

Your explorer connects to the server and retrieves the data. Or, if the connection is kept alive, it uses live.

0
source

Source: https://habr.com/ru/post/1302582/


All Articles