简体   繁体   English

Javascript 我无法使用 ajax 添加新段落

[英]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 (没有“demo2”)工作正常,在浏览器中我可以看到它在“PruebasGeneral/MBVR000008.txt”上的文本,当我改变这个文件/文本时,在我的HTML中工作而不刷新,但我需要添加其他

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. 如您所见,我尝试添加相同的功能,但不起作用,浏览器中的这段代码在两段中显示了“PruebasGeneral/MBVR000009.txt”中的内容,因此基本上显示了demo2和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:你不能只调用open()两次:

 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Javascript将HTML中的段落替换为新段落 - Replace paragraph in HTML with new paragraph using Javascript 我如何修复我的代码以将每个段落放在一个新行中,因为我试图在给定段落之前添加段落 - How can i fix my code to put each paragraph in a new line, as I am trying to add paragraph before the given paragraph JavaScript - 为什么我不能向“字符串”对象添加新属性? - JavaScript - Why can't I add new attributes to a “string” object? 如何使用正则表达式用新行替换段落元素? - How can I replace paragraph elements with new lines using regex? 使用createElement将新段落添加到现有段落并保持相同的CSS - using createElement to add a new paragraph to existing paragraph and keeping same css 为什么我不能使用javascript显示新图像? - Why can't I display a new image using javascript? 无法用 javascript 替换 HTML 段落中的文本 - Can't replace the text in an HTML paragraph with javascript ajax返回调用可以在页面上添加新的javascript函数吗? - Can ajax call on return add new javascript function to the page? 我怎样才能:用 JavaScript 选择一个段落(点击)? - How can I: Select a paragraph with JavaScript (on click)? 如何使用 javascript 更改段落的颜色 - How can I change the color of a paragraph with javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM