We introduced a sub-query in the previous article, but sometimes we need to transfer values between the page and the background or page and page, so we need to use a very important technical--ajax, presumably everyone has heard about it, today we introduce the AJAX, See how it plays the role of a transfer machine (we use MVC).
-1 concept
We all know that Ajax has two transmission strategies, that is synchronous and asynchronous, then what is synchronization, what is asynchronous, what is the difference?
Synchronization: Submit request, wait for server processing, processing completed returns this period the client browser cannot do anything
Asynchronous: Requests are processed through event triggering server processing (which is still something the browser can do)
For example: Synchronization is when you ask me to eat, I hear you go to dinner with you, if you do not hear, you keep barking until I tell you to hear, to go to dinner together. Async is you call me, and then go to dinner, I can get the news immediately, may wait until after work to eat.
-2 Realization Mode
In fact, Ajax implementation of synchronous and asynchronous is very simple, as long as you set the value of the Async property on the line, the default setting value is True, this situation is asynchronous, that is, when the Ajax sends the request, in the process of waiting for the server side to return, the foreground will continue to execute the script behind the Ajax block, It is not until the server side returns the correct result to execute the success, which means that this time the execution is two threads, the AJAX block makes the request after a thread and the AJAX block behind the script (another thread), two threads do not affect each other
Let's take a look at a JS code
<script type="Text/javascript"> //When the Asyn is set to False, then the AJAX request is synchronous, that is, when the AJAX block makes the request, he will wait in the function1 () this place, will not go to execute function2 (), until Function1 () Partial execution is complete. //When the Asyn is set to true, when the AJAX request is asynchronous, when the Ajax block makes the request, he will stay function1 (), waiting for the server side to return, but at the same time (during this wait), the foreground will go to execute function2 () function addclick() {//Click to trigger AddClick event //Use jquery to get the value of the page tag with ID txtusername, stored in the txtUserName variable vartxtUserName = $ ("#txtuserName"). Val ();varTxtuserpwd = $ ("#txtuserPWD"). Val (); $.ajax ({type:"POST",//ajax mode is post (get mode limits the length of the transmitted data)Url:"/ajaxrequest/adduser.ashx",//General Handler page adduser.ashx (the page content will be written out in 2)DataType:"JSON",//Data is returned in JSON formatData: {"txtUserName": txtUserName,"Txtuserpwd": Txtuserpwd},//or data: "txtUserName" + txtusername + "&txtuserpwd" + txtuserpwd, //Data key value to be transferred to Addusername key (convenient for files in 2 to accept data with this name) txtUserName to a value (variable to pass, such as user name)Success function (Result) {Function1 ()}); Function2 (); } ); function function1() {Alert' function1 '); } function function2() {Alert' function2 '); } })}</script>
After our JS code is finished, how to receive the data from the page in the background.
StringRequest["txtuserName"];StringRequest["txtuserPWD"];
-3 Summary
Today we learned how Ajax uploads data from a page to the background, and how the backend receives it. I still know that Ajax two different transmission methods, but also the core of Ajax, that is synchronous and asynchronous, we in the actual application must be based on their own needs to choose a reasonable synchronization or asynchronous. In the future study, we will also use Ajax, this time only introduced its post value of the way, in fact, he also has get way to pass the value, we can search the Internet, and then share with you.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Front page--ajax What kind of transfer machine?