JavaScript AJAX Stream Waterfall Streaming display instance

Source: Internet
Author: User
Tags html form

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.

The code is as follows Copy Code

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.

The code is as follows Copy Code
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.
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.