Ajax XMLHttpRequest Post get

Source: Internet
Author: User

/* When there are Chinese parameters, the receiver needs to use the UTF-8 method to decode the data
* Post attachments are not supported.
*/

Function getxmlhttprequest (){
VaR XMLHttpRequest = NULL;
Try {
XMLHttpRequest = new activexobject ("msxml2.xmlhttp ");
}
Catch (E1 ){
Try {
XMLHttpRequest = new activexobject ("Microsoft. XMLHTTP ");
} Catch (E2 ){
XMLHttpRequest = NULL;
}
}
If (XMLHttpRequest = NULL & typeof (XMLHttpRequest )! = 'Undefined '){
XMLHttpRequest = new XMLHttpRequest ();
}
Return XMLHttpRequest;
}

/* Use post to send data
* URL: Submit path
* Arguments: parameters, such as name = jese & sex = womanz. Use 'name = '+ getencodeuricomponent ('lily') for Chinese data ') + '& sex =' + getencodeuricomponent ('male') Encoding
* Synchflag: Synchronous flag. "false" indicates the synchronous mode, and "true" indicates the asynchronous mode.
* Returntype: returned content type 0 = responsebody; 1 = responsestream; 2 = responsetext; 3 = responsexml
* Execute: the callback function submitted asynchronously. This function is used to process the returned data. The format is function (OBJ) {}, and obj is the returned content.
*/

Function postdata (URL, arguments, synchflag, returntype, execute ){
VaR XMLHttpRequest = getxmlhttprequest ();
If (XMLHttpRequest = NULL) return false;
XMLHttpRequest. Open ("Post", URL, synchflag );
XMLHttpRequest. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
XMLHttpRequest. setRequestHeader ("Content-Length", arguments. Length );
If (synchflag ){
XMLHttpRequest. onreadystatechange = function (){
If (XMLHttpRequest. readystate = 4 ){
Switch (returntype ){
Case 0: Execute (XMLHttpRequest. responsebody); break;
Case 1: Execute (XMLHttpRequest. responsestream); break;
Case 2: Execute (XMLHttpRequest. responsetext); break;
Case 3: Execute (XMLHttpRequest. responsexml); break;
Default: Alert ("error ");
}
}
}
}
XMLHttpRequest. Send (arguments );
If (! Synchflag ){
Switch (returntype ){
Case 0: Return XMLHttpRequest. responsebody; break;
Case 1: Return XMLHttpRequest. responsestream; break;
Case 2: Return XMLHttpRequest. responsetext; break;
Case 3: Return XMLHttpRequest. responsexml; break;
Default: Return false;
}
}
}

/*
* Use get to send data
* URL: Submit path
* Arguments: the parameter, such as name = jese & sex = male. The Chinese data department must be encoded.
* Synchflag: Synchronous flag. "false" indicates the synchronous mode, and "true" indicates the asynchronous mode.
* Returntype: returned content type 0 = responsebody; 1 = responsestream; 2 = responsetext; 3 = responsexml
* Execute: the callback function submitted asynchronously. This function is used to process the returned data. The format is function (OBJ) {}, and obj is the returned content.
*/

Function getdata (URL, arguments, synchflag, returntype, execute ){
VaR XMLHttpRequest = getxmlhttprequest ();
If (XMLHttpRequest = NULL) return false;
XMLHttpRequest. Open ("get", URL + "? "+ Arguments, synchflag );
If (synchflag ){
XMLHttpRequest. onreadystatechange = function (){
If (XMLHttpRequest. readystate = 4 ){
Switch (returntype ){
Case 0: Execute (XMLHttpRequest. responsebody); break;
Case 1: Execute (XMLHttpRequest. responsestream); break;
Case 2: Execute (XMLHttpRequest. responsetext); break;
Case 3: Execute (XMLHttpRequest. responsexml); break;
Default: Alert ("error ");
}
}
}
}
XMLHttpRequest. Send ();
If (! Synchflag ){
Switch (returntype ){
Case 0: Return XMLHttpRequest. responsebody; break;
Case 1: Return XMLHttpRequest. responsestream; break;
Case 2: Return XMLHttpRequest. responsetext; break;
Case 3: Return XMLHttpRequest. responsexml; break;
Default: Return false;
}
}
}

/*
* Post Chinese data is used, and get data is not used
*/
Function getencodeuricomponent (arguments ){
Return encodeuricomponent (arguments );
}

/*
* Example
* Alert (postdata ('t. jsp ', 'name =' + getencodeuricomponent ('shen Shen '), false, 2, null ));
* Alert (getdata ('t. jsp ', 'name = Day', false, 2, null ));
* Postdata ('t. jsp ', 'name =' + getencodeuricomponent ('shen Shen '), true, 2, function (OBJ) {alert (OBJ );});
* Getdata ('t. jsp ', 'name = Day', true, 2, function (OBJ) {alert (OBJ );});
*/

Related Article

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.