Proficient in Ajax skills

Source: Internet
Author: User
Writing an article: I am no longer familiar with Ajax skills (reprinted). maybe it is (ActiveJavascriptActionXml, to put it bluntly, javascript, xmlhttp, and xmldom techniques and website background are used for processing. Writing articles: proficient in Ajax skills (reproduced)

I can't help myself with the Ajax name, probably (Active Javascript Action Xml, to put it bluntly, javascript, xmlhttp, and xmldom techniques and website background are applied to handle some of your control methods.

I will illustrate how to apply Ajax skills for development in three steps.

1. use javascript to control xmlhttp objects

2. server department's response to xmlhttp request (PHP model)

3. xmldom application method


Let's talk less about it. let's talk about the first part:

1. use javascript to control xmlhttp objects

In IE7, Mozilla, Firefox, and other browsers, javascript has the built-in XMLHttpRequest object, but IE5 does not. you need to start it using the following method:
// IE 6
Try {xhttp = new ActiveXObject ('msxml2. xmlhttp';} catch (e ){;}
// IE5
If (xhttp = null) try {xhttp = new ActiveXObject ('Microsoft. xmlhttp';} catch (e ){;}

Consider the compatibility of different browsers. Generally, the following method is required to start an xmlhttp:

CODE: [Copy to clipboard] var xhttp = null;

If (window. XMLHttpRequest) {// IE7, Mozilla, Firefox, and other browsers have built-in objects.

Xhttp = new XMLHttpRequest ();

} Else if (window. ActiveXObject) {// IE6, IE5

Try {xhttp = new ActiveXObject ('msxml2. xmlhttp');} catch (e ){;}

If (xhttp = null) try {xhttp = new ActiveXObject ('Microsoft. xmlhttp');} catch (e ){;}

} Xmlhttp applications are generally in the following order:

1. initialize the xmlhttp object (above );

2. open the link

Method
Xhttp. open ('GET', purl, true );

Parameter 1: Use the GET or POST method to send data

Parameter 2: Request URL (only resources on your server can be requested. generally, browser security restrictions cannot read Cross-origin data)

Parameter 3. true indicates asynchronous transmission (you can perform other control before the server returns information). false indicates the transmission of blocking methods.

3. set the http request header to be sent

Method:

Xhttp. setRequestHeader (key, value );

Generally, the default headers to be sent are:
Xhttp. setRequestHeader ('content-type', 'application/x-www-form-urlencoded ';

This type of request header is used to send text data, and javascript is sent in unicode by default.

Another situation is:
Xhttp. setRequestHeader ('content-type', 'multipart/form-data ';

It indicates the sending of binary data. for security reasons, javascript generally cannot use this method to send data, so this header is generally useless.

If your website has enabled the refer parameter for anti-Leech protection, you must use this method to specify the Refer parameter, or if the user needs to log on for a certain hold, then you must specify the Cookie request header.

4. send data

Method: xhttp. send (postdata );

For the request to use get to attack the hand, you do not need to specify postdata, directly use test. php? A = a & B = B.

If it is the post method, you need to use key1 = value2 & key2 = value2 to process the data, merge it into the postdata string, and then send it.

Considerations:

By default, javascript uses unicode to send data, and the returned data must be in UTF-8 format. Therefore, when sending data, you need to use the escape () function to process postdata and URL values, these values must be restored on the server, and unicode is converted to page encoding values. Therefore, if jsp or asp.net is used, it will be relatively simple, but if php is used to process it, I will teach you how to do it later.


5. confirm that the materials returned by the server are downloaded.

[1] if a request is sent using a blocking method, you can use if (xhttp. readyState = 4) to determine whether the request is complete.

The specific attribute value of readyState is:
0: No open
1 No send
2 Unknown status
3. Transmitting
4 Transfer completed

Of course, for the sake of protection, we need to add a lot of judgment.

Is if (xhttp. status = 200)

Status is the response header code in the http protocol.

1xx performance (sorry, I forgot)
2xx successful message
3xx performance page transfer
The 4xx page does not exist.
5xx Server errors

If your page is not specially processed, we usually use if (xhttp. status = 200) to make sure that the returned content is accurate.


[2] If asynchronous transmission is used, the onreadystatechange event must be used for listening.

Xhttp. onreadystatechange = function ()
{
// Determine the above blocking method
If (myajax. xhttp. readyState = 4 ){
If (myajax. xhttp. status = 200 ){
// Subsequent control
}
}
}

6. get returned results

Attribute:
[1] xhttp. responseBody;
[2] xhttp. responseStream;
[3] xhttp. responseXml;
[4] xhttp. responseText;

Among them, 1 and 2 are binary methods, which are rarely used, and 4 is not needed.

If the server is not surprised, [3] returns an xmldom object.

2. server department's response to xmlhttp request (PHP model)

To simplify the control, the xmlhttp operations are encapsulated as a class.

CODE: [Copy to clipboard] function DedeAjax (WiteOKFunc) {// WiteOKFunc is an asynchronous state event handler function

// Xmlhttp and xmldom objects
This. xhttp = null;
This. xdom = null;

// Key-value pairs for sending data via post or get
This. keys = Array ();
This. values = Array ();
This. keyCount =-1;

// Http request header
This. rkeys = Array ();
This. rvalues = Array ();
This. rkeyCount =-1;
// Request header type
This. rtype = 'text ';

// Initialize xmlhttp
If (window. XMLHttpRequest) {// IE7, Mozilla, Firefox, and other browsers have built-in objects.
This. xhttp = new XMLHttpRequest ();
} Else if (window. ActiveXObject) {// IE6, IE5
Try {this. xhttp = new ActiveXObject ('msxml2. xmlhttp');} catch (e ){}
If (this. xhttp = null) try {this. xhttp = new ActiveXObject ('Microsoft. xmlhttp');} catch (e ){}
}
This. xhttp. onreadystatechange = WiteOKFunc;
// Rs: responseBody, responseStream, responseXml, and responseText

// The following are member functions.
//--------------------------------

// Initialize xmldom
This. InitXDom = function (){
Var obj = null;
If (typeof (DOMParser )! = 'Undefined') {// Gecko, Mozilla, Firefox
Var parser = new DOMParser ();
Obj = parser. parseFromString (xmlText, 'text/XML ');
} Else {// IE
Try {obj = new ActiveXObject ('msxml2. domdocument');} catch (e ){}
If (obj = null) try {obj = new ActiveXObject ('Microsoft. XMLDOM ');} catch (e ){}
}
This. xdom = obj;
};

// Add a POST or GET key-value pair
This. AddKey = function (skey, svalue ){
This. keyCount;
This. keys [this. keyCount] = skey;
This. values [this. keyCount] = escape (svalue );
};

// Add an Http request header key-value pair
This. AddHead = function (skey, svalue ){
This. rkeyCount;
This. rkeys [this. rkeyCount] = skey;
This. rvalues [this. rkeyCount] = svalue;
};

// Clear the hash table parameters of the current object
This. ClearSet = function (){
This. keyCount =-1;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.