Ajax client instructions, XMLHttpRequest objects

Source: Internet
Author: User
Tags object eval execution header http request return string window
Ajax|request|xml|xmlhttprequest| Object | client Use your own AJAX mechanism in CommunityServer without the help of other auxiliary controls. The encapsulation of the customer's XMLHttpRequest object can be a feast for the eyes, and it is capable of running Ajax in a typical browser. Here we learn to learn this drum, hoping to bring more people to help.
First of all, of course, to understand the XMLHTTP object in the browser:
xmlHTTP Method:
Note: Clients can use the XMLHTTP object to send arbitrary HTTP requests, accept HTTP replies, and parse XML documents that are answered.
Open method: Initializes a msxml2.xmlhttp request, specifying the HTTP request method, URL, and authentication information.
Grammar:
Open (Bstrmethod, bstrURL, Varasync, Bstruser, Bstrpassword)
Parameter introduction:
Bstrmethod: Data transfer mode, i.e. get or post.
bstrURL: URL of the Service Web page.
Varasync: Whether to execute synchronously. The default is true, which is synchronous execution, but can only be implemented synchronously in the DOM. It is usually set to false in, that is, asynchronous execution.
Bstruser: User name, can be omitted.
Bstrpassword: User password, can be omitted.
Send method: Sends an HTTP request to the server and returns an answer.
Grammar:
Oxmlhttprequest.send (Varbody)
Description: Whether this method synchronizes depends on the Varasync parameter of the Open method. If set to True, the call returns immediately, and returns if the call is set to false until the entire answer is received.
setRequestHeader (Bstrheader, Bstrvalue)
Bstrheader:http Header (header)
Value of Bstrvalue:http header (header)
If the open method is defined as post, you can define a form to upload:
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded")
XMLHTTP Properties:
onReadyStateChange: Gets the event handle that returns the result in the synchronous execution mode. Can only be invoked in the DOM.
Responsebody: The result returns an array of unsigned integers.
Responsestream: The result is returned as a IStream stream.
ResponseText: The result is returned as a string.
Responsexml: Results are returned as XML format data.
Use this principle can also do network thief program, web crawler should be the application of this thing to complete it, but I did not do, probably in the near future will be made to play, here we are the most important to see how the CS encapsulation of his:
1//ajax Start
2/**////
3///creates a callback object if there is a window. XMLHttpRequest () object, returns this object and, if it is IE, searches msxml2.xmlhttp versions and Microsoft.XMLHTTP and creates object returns.
4///
5function Ajax_getxmlhttprequest () {
6 if (window. XMLHttpRequest) {
7 return new XMLHttpRequest ();
8} else {
9 if (window. Ajax_xmlhttprequestprogid) {
Return to new ActiveXObject (window. Ajax_xmlhttprequestprogid);
} else {
var ProgIDs = ["msxml2.xmlhttp.5.0", "msxml2.xmlhttp.4.0", "MSXML2". xmlhttp.3.0 "," MSXML2. XMLHTTP "," Microsoft.XMLHTTP "];
for (var i = 0; i < progids.length; ++i) {
var ProgID = progids[i];
try {
var x = new ActiveXObject (ProgID);
Window. Ajax_xmlhttprequestprogid = ProgID;
return x;
catch (e) {
}
}
}
}
return null;
}
/**////
Ajax callbacks.

the class that calls the service-side function includes namespaces (such as: NExplus.Controls.SiteHeader).
Client (such as:
).
Service-side (methods) function name (marked by Ajaxmethod).
the string passed to the server.
Synchronous or asynchronous Callbacks.
Debug/Request String.
Debug/Output String.
Debugging.
is callback with the control and its value.
url address.
function Ajax_callback (type, id, method, args, Clientcallback, Debugrequesttext, Debugresponsetext, Debugerrors, Includecontrolvalueswithcallback, URL) {
if (!url)
{
url = window.location.href;
url = url.replace (/\#.*$/, ');//Remove the label part of the URL, that is, the string after "#".
Add parameter Ajax_callback and set to true to indicate an AJAX callback.
if (Url.indexof ('? ') >-1)
URL + "&ajax_callback=true";
Else
{
if (Url.substr (url.length-1, 1) = = "/")
URL + "default.aspx";
url = "? Ajax_callback=true ";
}
}
var x = Ajax_getxmlhttprequest ();//Get XMLHttpRequest object.
var result = null;
if (!x) {
result = {"value": null, "error": "Noxmlhttp"};
if (debugerrors) {
Alert ("Error:" + result.error);
}
if (clientcallback) {
Clientcallback (result);
}
return result;
}
X.open ("Post", url, Clientcallback-true:false);//Open the object as POST so that the parameters can be obtained with Request.Form on the server side.
X.setrequestheader ("Content-type", "application/x-www-form-urlencoded; Charset=utf-8 ");
if (clientcallback) {
If sync, judge status, output error message.
X.onreadystatechange = function () {
var result = null;
if (x.readystate!= 4) {
Return
}
if (Debugresponsetext) {
alert (X.responsetext);
}
Try
{
var result = eval ("+ X.responsetext +"));
if (debugerrors && result.error) {
Alert ("Error:" + result.error);
}
}
catch (Err)
{
if (Window.confirm (' The following error occured while processing an AJAX request: ' + err.message + ' \n\nwould The response? ')
{
var w = window.open ();
W.document.open (' Text/plain ');
W.document.write (X.responsetext);
W.document.close ();
}
result = new Object ();
Result.error = ' An AJAX error occured. The response is invalid. ';
}
Clientcallback (result);
}
}
var encodeddata = "ajax_callbacktype=" + type;
if (ID) {
Encodeddata + + "&ajax_callbackid=" + id.split ("$"). Join (":");
}
Encodeddata + + "&ajax_callbackmethod=" + method;
if (args) {
for (var i in args) {
Encodeddata + + "&ajax_callbackargument" + i + "=" + encodeURIComponent (args[i));
}
}
If the control is joined, the control data is added.
if (includecontrolvalueswithcallback && document.forms.length > 0) {
var form = document.forms[0];
for (var i = 0; i < form.length; ++i) {
var element = Form.elements[i];
if (element.name) {
var elementvalue = null;
if (Element.nodename = = "INPUT") {
var inputtype = Element.getattribute ("TYPE"). toUpperCase ();
if (InputType = = "TEXT" | | inputtype = = "PASSWORD" | | inputtype = = "HIDDEN") {
Elementvalue = Element.value;
else if (inputtype = "checkbox" | | inputtype = = "RADIO") {
if (element.checked) {
Elementvalue = Element.value;
}
}
else if (element.nodename = = "Select") {
Elementvalue = Element.value;
else if (Element.nodename = = "TEXTAREA") {
Elementvalue = Element.value;
}
if (elementvalue) {
Encodeddata + + "&" + element.name + "=" + encodeURIComponent (elementvalue);
}
}
}
}
If it is debugging, the data that is sent is ejected.
if (Debugrequesttext) {
alert (encodeddata);
}
X.send (encodeddata);//Send data to the server.
if (!clientcallback) {
if (Debugresponsetext) {
alert (X.responsetext);
}
result = eval ("+ X.responsetext +"));
if (debugerrors && result.error) {
Alert ("Error:" + result.error);
}
}
Delete x;
return result;
}
Ajax End
Other do not need more explanation, look at the annotation should be almost, if there is a wrong place please criticize, thank you!


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.