javascript The Definitive Guide Chapter 21 Ajax and Comet

createXHR function () { 
    IF (typeof the XMLHttpRequest = 'undefined'!) { 
        return new new the XMLHttpRequest (); 
    } the else IF (! = typeof the ActiveXObject 'undefined') { 

        IF (! = typeof arguments.callee.ActiveXString 'String') { 

            versions = var [ 'MSXML2.XMLHttp.6.0', 'MSXML2.XMLHttp.3.0', 'Msxml2.XMLHTTP'], I, len; 
            
            for (I = 0, len = versions.length; I <len; I ++) { 
                {the try 
                    // determines whether the current version can be successfully created ActiveObject 
                    new new the ActiveXObject (versions [I]); 
                    arguments.callee.ActiveXString versions = [I]; // if the current object parameter set 
                    break;
                } catch (error) {
            } 
                    //jump over
                } 
        } 
        Return new new ActiveXObject (arguments.callee.ActiveXString); // Create ActiveXObject according to the parameters of the current function of 
    } 
    the else { 
        the throw new new Error ( 'No Object are avilable the XHR.'); 
    } 
} 

// Create XHR 

var XHR = createXHR () ; 

xhr.onreadystatechange = function () { 
    IF (xhr.readySate ==. 4) { 
        IF (xhr.status> = 200 is && xhr.status <300 || xhr.status == 304) { 
            var = xhr.responseText the responseText; 
        the else {} 
            // the request is not a normal consequence 
            Alert ( 'the request WAS unsuccessful:' + xhr.status); 
        } 
    } 
} 
xhr.open ( 'gET', 'the example.php', to false);// parameter get / post url async
xhr.send (null); // send data body transmission request received, it may be provided if get null 
xhr.abort (); // cancellation request
 
//21.1.2 HTTP header information

xhr.setRequestHeader ( 'MyHeader', 'MyValue '); // header information setting request 

// post submitted 
xhr.open ( 'POST', 'URL', to true); 
var form = document.getElementById ( 'User-info' ); 
xhr.send (the serialize (form)); // serialized form form data and submit the form 

//21.2.1 the FormData 
// xmlhttprequest submitted to optimize the post defines the FormData 
var the FormData new new data = (); 
data.append ( 'name', 'Nicholas'); 
xhr.send (Data); 

var form = document.getElementById ( 'User-info'); 
xhr.send (the FormData new new (form)); 

//21.2.2 timeout 
xhr .timeout = 1000; // set one second 
xhr.ontimeout = function () { 
    Alert ( 'Not return in the Request DID A SECOND'); 
} 

//21.2.3 overrideMimeType () method 
xhr.overrideMimeType ( 'text / xml '); // Forced request object in response to the presence of the process instead of plain text XML
 
//21.3 progress event 

// loadstart trigger on the first byte of the received response data 
// progress during continuous receive a response trigger 
// error Raised for a request error. 
// abort because the call is triggered when the abort () method to terminate the link 
// load triggered upon receiving a complete response data 
// loadend communication is completed or after the trigger event triggered error abort or load 

xhr.onload = function () { 

} 
xhr.onprogress = function () { 

} 

//21.4 origin resource sharing across 

@ domain security server is provided, the requester may be provided not cross-domain 
// Access-Control-Allow-origin : http: // www .nczonline.net 
// If the server authorized the request source, in the header information postback same source or may send back-Control-the Allow-Access origin: "*" 

//21.4.1 IEs achieve the CORS 

XDR new new XDomainRequest = var (); 

//21.4.3 preflighted requests 
// request header by setting information so that the server determines whether the request is supported
The Allow Access-Control-//-Origin: HTTP: //www.nczonline.net 
// Access-Control-the Allow-Methods: POST, GET 
// Access-Control-the Allow-Headers: NCZ 
// Access-Control-the Allow -Age: 1728000 

request credentials //21.4.4 with 
// the Allow-Access-Control-Crendentials: to true 


//21.5 other cross-domain techniques 
//21.5.1 image of ping 
 var IMG = new new image (); 
 img.onload = = function img.onerror () { 
     Alert ( 'Done!'); 
 } 
 img.src = 'HTTP:? //www.example.com/text name = Nicholas'; 


 //21.5.2 JSONP 
 // loaded by script way to achieve cross-domain, because the script with similar img can not cross-domain restrictions 
 // <script src = 'HTTP: // localhost:? 9090 / Student callback = showData'> <\ / script> 
 // server returns showData ({name: 'mas'} ); // construct a function JSONP executable format, back to the client executes a client function cross-domain showData
 
 //21.5.3 Comet 

 // see example https://www.iteye.com/blog/raising-2271869 



 //21.5.4 server send events

 The EventSource new new Source = var ( 'URL'); 
 source.open (); 
 source.onmessage = function (Event) { 
     var Data = the event.data; 
 }; 
 source.close (); 


 // the Web Sockets 21.5.5 

 var Socket = new WebSocket ( 'ws: //www.example.com/server.php '); 
 // after WebSocket object is instantiated, the browser will immediately attempt to establish a link, websocket there is also a representative of the current state 
 // readyState properties values were WebSocket.OPENING (0) is to establish a link 
 //WebSocket.OPEN(1) has established a link 
 //WebSocket.CLOSEING(2) being closed link 
 //WebSocket.CLOSE(3) has closed the connection 

 socket.send ( ' World Hello ');! 
 var message = { 
     Time: new new a Date (), 
 socket.send (the JSON.stringify (message)); 
 // receiving a message from the server 
     text: "the Hello World!",
     clientId: 'asdfp8734rew' 
 }; 
 socket.onmessage = function (Event) { 
     var Data = the event.data; 
     // processing logic 
 }; 
 
 socket.onopen = function (Event) { 

 }; 
 socket.onerror = function (Event) { 

 } ; 
 socket.onclose = function (event) { 
     // event three OnClose specific 
     //event.wasClean(boolean value indicating whether closed) 
     // event.code (numeric status code returned by the server) 
     // event.reason (message returned by the server) 
 }

  

Guess you like

Origin www.cnblogs.com/ms_senda/p/11520183.html
Recommended