JavaScript AJAX Stream Streaming display

Source: Internet
Author: User
Tags html form

When using Ajax for information interaction, if the information returned by the server is relatively large, the streaming display is more friendly than the unified display after the transfer is complete.


Streaming implementation
The principle is to set the timer, periodically check the status of the Ajax object and update the content, if the transfer is complete, cancel the timer.
  1. function Ajax_stream (url,data,element) {
  2. var xmlhttp=null;
  3. if (window. XMLHttpRequest)
  4. {//code for IE7, Firefox, Opera, etc.
  5. Xmlhttp=new XMLHttpRequest ();
  6. }
  7. else if (window. ActiveXObject)
  8. {//code for IE6, IE5
  9. Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
  10. }
  11. if (xmlhttp==null)
  12. {
  13. Alert ("Your browser does not support XMLHTTP.");
  14. Element.val (' Your browser does not support XMLHTTP. Click the LOG link to monitor the procedure. ');
  15. return 0;
  16. }
  17. var xhr = xmlHttp;
  18. Xhr.open (' POST ', url, true);
  19. 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:
  20. Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
  21. Xhr.send (data);
  22. var timer;
  23. Timer = Window.setinterval (function () {
  24. if (xhr.readystate = = Xmlhttprequest.done) {
  25. Window.cleartimeout (timer);
  26. }
  27. Element.val (Xhr.responsetext);
  28. }, 1000);
  29. }


Post Data conversion
Because send in the standard implementation can only accept the following types of inputs, it is better to convert the data objects that need to be passed into a string or formdata format in advance, which is not asJQuery, but how jquery can implement event response in the middle of the transmission is not known, so it cannot be used, or all objects can be converted to JSON.
    1. void Send ();
    2. void Send (ArrayBuffer data);
    3. void Send (Blob data);
    4. void Send (Document data);
    5. void Send (domstring data);
    6. void Send (FormData data);
The following is the code of the conversion, if the browser supports Formdata conversion, otherwise it will be converted into a string.
  1. function Ajax_generate_data (jsobj) {
  2. var i;
  3. if (window. FormData) {
  4. var data = new FormData ();
  5. For I in Jsobj {
  6. Data.append (I,jsobj[i]);
  7. }
  8. } else {
  9. var data = ';
  10. var datas = [];
  11. For I in Jsobj {
  12. For the values so, possible & contained in the strings does not break the format
  13. var value = encodeURIComponent (Jsobj[i]);
  14. Datas.append (i + ' = ' + value);
  15. }
  16. data = Datas.join (' & ')
  17. }
  18. Console.log (data);
  19. return data;
  20. }

JavaScript AJAX Stream Streaming display

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.