When using Ajax for information interaction, if the server returns a relatively large information, then relative to the completion of the transmission after the unified display, the flow-style display is more friendly.
Streaming implementations of
The principle is to set the timer, the time to view the state of the Ajax object and update the content, if the transfer is completed, cancel the timer.
Copy Code code as follows:
function Ajax_stream (url,data,element) {
var xmlhttp=null;
if (window. XMLHttpRequest)
{//code for IE7, Firefox, Opera, etc.
Xmlhttp=new XMLHttpRequest ();
}
else if (window. ActiveXObject)
{//code for IE6, IE5
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
if (xmlhttp==null)
{
Alert ("Your browser does not support XMLHTTP.");
Element.val (' Your browser does not support XMLHTTP. Click the LOG link to monitor the procedure.
return 0;
}
var xhr = xmlHttp;
Xhr.open (' POST ', url, true);
If you need to POST data like an HTML form, use setRequestHeader () to add an HTTP header. Then specify the data you want to send in the Send () method:
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xhr.send (data);
var timer;
Timer = Window.setinterval (function () {
if (xhr.readystate = = Xmlhttprequest.done) {
Window.cleartimeout (timer);
}
Element.val (Xhr.responsetext);
}, 1000);
}
Post Data conversion
Since send in a standard implementation can only accept several inputs, it is not as convenient as jquery to convert a data object that needs to be passed in advance to a string or formdata format, but it is not known how jquery can implement the incident response in the middle of the transmission. or convert all objects to JSON.
Copy Code code as follows:
void Send ();
void Send (Arraybuffer data);
void Send (Blob data);
void Send (Document data);
void Send (domstring data);
void Send (FormData data);
The following is the converted code, which is converted if the browser supports Formdata, otherwise it is turned into a string.
Copy Code code as follows:
function Ajax_generate_data (jsobj) {
var i;
if (window. FormData) {
var data = new FormData ();
For I in Jsobj {
Data.append (I,jsobj[i]);
}
} else {
var data = ';
var datas = [];
For I in Jsobj {
For the "values so" possible & contained in the strings does not break the format
var value = encodeURIComponent (Jsobj[i]);
Datas.append (i + ' = ' + value);
}
data = Datas.join (' & ')
}
Console.log (data);
return data;
}