An Ajax class

Source: Internet
Author: User
Tags date format object return
Ajax

An Ajax class
function Ajax (URL,RECVT,STRINGS,RESULTF) {
This.url = URL;
This.strings = StringS;
This.xmlhttp = This.createxmlhttprequest ();
if (this.xmlhttp = = null) {
Alert ("Erro");
Return
}
var objxml = this.xmlhttp;
Objxml.onreadystatechange = function () {Ajax.handlestatechange (OBJXML,RECVT,RESULTF)};
}

Ajax.prototype.createXMLHttpRequest = function () {
try {return new ActiveXObject ("Msxml2.xmlhttp"); catch (e) {}
try {return new ActiveXObject ("Microsoft.XMLHTTP");} catch (e) {}
try {return new XMLHttpRequest (); catch (e) {}
return null;
}

Ajax.prototype.createQueryString = function () {
var querystring = this.strings;
return querystring;
}

Ajax.prototype.get = function () {
url = this.url;
var querystring = url+ "? timestamp=" + New Date (). GetTime () + "&" + this.createquerystring ();
This.xmlHttp.open ("Get", querystring,true);
This.xmlHttp.send (NULL);
}

Ajax.prototype.post = function () {
    url = this.url;
    var url = url + "? timestamp=" + New Date (). GetTime ();
    var querystring = this.createquerystring ();
    this.xmlHttp.open ("POST", url,true);
    this.xmlHttp.setRequestHeader ("Content-type", "application/x-www-form-urlencoded");
    This.xmlHttp.send (querystring);
}
   
Ajax.handlestatechange = function (XMLHTTP,RECVT,RESULTF) {
    if (xmlhttp.readystate = 4) {
        if (xmlhttp.status = =) {
         RESULTF (Recvt?xmlhttp.responsexml:xmlhttp.responsetext);
       } else {
        alert (" The page you are requesting has an exception. ");
       }
   }
}

How to use
var classajax = new Ajax (URL,RECVT,STRINGS,RESULTF);
Classajax.post (); Send data in//post mode
Classajax.get (); Send data in//get mode
URL send address recvt accept data type 0 FOR XML 1 for text RESULTF return result handler

Cases
var ajax1 = new Ajax ("1.asp", 0, "id=" +id,bacal);
Ajax1.post ();
function Bacal (REXM) {
Returns the data processing function;
}
-----------------------------------------------------------

Quertion:
One of the code RESULTF (Recvt?xmlhttp.responsexml:xmlhttp.responsetext);
Do not understand, there is the eldest brother to explain the explanation?
When RECVT is 0 o'clock, it returns an object, I don't know how to use it, or what attribute does the object have?
The middle of this sentence? And: What's the format here, what role?

Answer:
Ternary operator.
(a>b)? 1:0
It means: If a>b, then 1 or 0.

RESULTF (Recvt?xmlhttp.responsexml:xmlhttp.responsetext);
by literal means:
Accept data function: RESULTF
Data received: RECVT (should be abbreviated as recived text)
The returned data is returned in Xmlhttp.responsexml or Xmlhttp.responsetext format.



Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.