简体   繁体   中英

Javascript i can't add a new paragraph using ajax

Hi im having this problem, when i use the code only for the

(without the "demo2" ) works fine, and in the browser i can see the text that its on "PruebasGeneral/MBVR000008.txt" and when i change this file/text, works in my HTML without refreshing, but i need to add another

as you can see, i tried to add in the same function, but doesnt work, with this code in the browser in the two paragraph shows whats inside "PruebasGeneral/MBVR000009.txt" so basically shows demo2 and demo2. WHAT SHOULD I DO?

 <!DOCTYPE html> <html> <body> <p id="demo"></p> <p id="demo2"></p> <script> function loadDoc(path, callback) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { callback(this.responseText); } }; xhttp.open("GET", path + "?t=" + Math.random(), true); xhttp.send(); } function data1Loaded(data) { document.getElementById("demo").innerHTML = data ; // do something with data } function data2Loaded(data) { document.getElementById("demo2").innerHTML = data ; // do something with data } function loadDocs() { loadDoc('/PruebasGeneral/MBVR000008.txt', data1Loaded); loadDoc('/PruebasGeneral/MBVR000009.txt', data2Loaded); setTimeout(loadDocs, 1000); } window.onload = loadDocs; </script> </body> </html>

You need to have all of that over again. You can't just call open() twice:

 function loadDoc(path, callback) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { callback(this.responseText); } }; xhttp.open("GET", path + "?t=" + Math.random(), true); xhttp.send(); } function data1Loaded(data) { // do something with data } function data2Loaded(data) { // do something with data } function loadDocs() { loadDoc('path1', data1Loaded); loadDoc('path2', data2Loaded); setTimeout(loadDocs, 1000); } window.onload = loadDocs;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM