See Ajax. Here we will first look at how to write an Ajax file with javascrui.
The principle of AJAX is the XMLHTTPRequest object.
First, we need to create this object
First, create an xajax. js file.
1 var request = false;
2
3 try {
4
5 request = new XMLHttpRequest();
6
7 } catch (ex){
8
9 try {
10
11 request = new ActiveXObject("Msxml2.XMLHTTP");
12
13 } catch(ex1){
14
15 try {
16
17 request = new ActiveXObject("Microsoft.XMLHTTP");
18
19 } catch (failed) {
20
21 request = false;
22
23 }
24
25 }
26
27 }
28
29
Here we create an XMLHTTPRequest object. The unique purpose of the XMLHTTPRequest object is to send a request and accept the corresponding
Several common functions in Ajax
Open (): Create a new request to the server.
Send (): send a request to the server.
Abort (): exit the current request.
Readystate: Provides the ready status of the current HTML.
Responsetext: Request Response text returned by the server.
1 function getinfo (URL ){
2
3 request. Open ("get", URL, true); // setting true in the open function is the key to Asynchronization.
4
5 request. onreadystatechange = callback; // you can specify a callback function.
6
7 Request. Send (null); // send is used to pass a parameter. If no parameter exists, it is null.
8
9}
10
11
12
13 // now we implement this callback function
14
15 function callback (){
16
17 // first, let's determine the status
18
19 if (request. readystate = 4) {// 4 indicates the response is complete.
20
21 if (request. Status = 200) {// 200 indicates that the contract negotiation status is smooth, for example, we will see a 404 error.
22
23 // after the judgment is complete, we can process the value returned by the server.
24
25 $ ("# A"). Text (request. responsetext );
26
27
28
29}
30
31}
32
33}
34
35
In this way, we have basically completed a basic Ajax, and then we have created a hand. ashx processing program.
We place a button on the index. ASPX page and write onclick = "getinfo ('hand. ashx')" to complete a simple Ajax
When jquery appears, Ajax becomes a sentence.
How can I implement the above request jquery?
Let's take a look at the load function: load the remote HTML file code and insert it into the Dom,
Then we only need
$('#a').load('hand.ashx')
We can implement n multiple codes written above.
Of course, we know there are two methods: Get and post. jquery provides
$. Get (URL, [data], [callback], type) $. Post () two methods, respectively using simple get/post to replace complex $. Ajax
URL indicates that the address data is the input parameter callback. The callback function type indicates the type of the returned value.
Of course, sometimes this method cannot meet our needs. $. Ajax is used here.
In this function, you can easily set Ajax details by color, for example:
$.ajax({
type: "get",
url: "hand.ashx ",
success: function(text){
alert( text);
}
});
Of course, we do not want to repeatedly write these details every time we write $ Ajax. At this time, we will use
$ Ajaxsetup is used to set global Ajax default options. The parameter method is the same as that of $. Ajax.
Ajax is similar here, but sometimes we need to perform progress operations on AJAX. For example, please wait while requesting data and display data when the data request is complete.
Ajax events will be used here
Ajaxsend (callback) Ajax request execution before sending
Ajaxstart (callback) is executed when the Ajax request starts.
Executed when the ajaxcomplete (callback) Ajax request is complete
Jqueryajaxstop (callback) ajaxExecuted at request end
When the ajaxsuccess (callback) Ajax request is successful
Jqueryajaxerror (callback) is executed when an Ajax request error occurs.
Related code