JavaScript combines ajax_stream to realize streaming display _javascript technique

Source: Internet
Author: User
Tags html form

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;
}

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.