Although there is a framework, many Ajax calls can directly call their APIs. But in some extreme cases, such as interviews, such as third-party applications that do not want to load these libraries, we only have to write them by ourselves. At this time, I think many people will be crazy about Google and Baidu. There is too much junk information on the Internet. I 'd like to save it on my blog.
My implementation:
VaR Ajax = new (self. XMLHttpRequest | activexobject) ("Microsoft. XMLHTTP ") Ajax. onreadystatechange = function () {If (Ajax. readystate = 4 & Ajax. status = 200) {alert (Ajax. responsetext)} Ajax. open ("Post", URL, true); Ajax. send ("Key = Val & key1 = val2 ");
Implementation of the Dark Knight in the group:
Ajax = function () {function request (URL, OPT) {function FN () {} var async = opt. async! = False, method = OPT. method | 'get', Data = OPT. data | null, success = OPT. success | Fn, failure = OPT. failure | Fn; method = method. touppercase (); If (Method = 'get' & Data) {URL + = (URL. indexof ('? ') =-1? '? ':' & ') + Data; data = NULL;} var xhr = Window. XMLHttpRequest? New XMLHttpRequest (): New activexobject ('Microsoft. XMLHTTP '); xhr. onreadystatechange = function () {_ onstatechange (xhr, success, failure) ;}; xhr. open (method, URL, async); If (Method = 'post') {xhr. setRequestHeader ('content-type', 'application/X-WWW-form-urlencoded; ');} xhr. send (data); Return xhr;} function _ onstatechange (xhr, success, failure) {If (xhr. readystate = 4) {var S = xhr. status; If (s >= 200 & S <300) {success (xhr) ;}else {failure (xhr) ;}} else {}} return {request: request };}();