除了使用ActiveX,还有什么方法可以读取javascript中的文本文件?

除了使用ActiveX,还有什么方法可以读取javascript中的文本文件?,javascript,html,xmlhttprequest,Javascript,Html,Xmlhttprequest,如果所有文件(html文件、文本文件等)都在web上, 是否有任何方法可以读取文本文件并将其打印在ActiveX旁边的文本区域 我试过这样做,但没有达到目的: function getSelectedItem(){ var client = new XMLHttpRequest(); if(document.codeForm.dropList.value == "foo") client.open('GET', 'foo.txt'); else if(docume

如果所有文件(html文件、文本文件等)都在web上, 是否有任何方法可以读取文本文件并将其打印在ActiveX旁边的文本区域

我试过这样做,但没有达到目的:

function getSelectedItem(){ var client = new XMLHttpRequest(); if(document.codeForm.dropList.value == "foo") client.open('GET', 'foo.txt'); else if(document.codeForm.dropList.value == "bar") client.open('GET', 'bar.txt'); client.onreadystatechange = function() { //This actually displays the message in the file alert(client.responseText); //But this doesn't. This just displays "undefined" // document.codeForm.source.value = client.reponseText; } client.send(); } 函数getSelectedItem(){ var client=new XMLHttpRequest(); if(document.codeForm.dropList.value==“foo”) client.open('GET','foo.txt'); else if(document.codeForm.dropList.value==“bar”) client.open('GET','bar.txt'); client.onreadystatechange=函数(){ //这实际上会在文件中显示消息 警报(client.responseText); //但这不是。这只是显示“未定义” //document.codeForm.source.value=client.reponseText; } client.send(); } 因为我实际上可以用文件上下文显示警报消息,所以我相信有某种方法可以做到这一点。 (实际上,文件的内容似乎进入了“client.reponseText”, 但它的数据类型是DOMstring,而不仅仅是String。)

任何建议都将不胜感激。 谢谢

试试这个

document.codeForm.source.innerValue = client.reponseText;

您的textarea将需要一个id属性来使用最后两种方法


我刚试过,但文本区域没有任何变化。它们仍然显示“未定义”。我通过使用jQuery解决了这个问题。谢谢。@Mike Ruhlin-wat,如果txt和html文件都在本地系统上,则返回
document.getElementById("source").innerHtml = client.responseText;
document.getElementById("source").innerText = client.responseText;
$.get("http://www.whatever.com/foo.txt", null, function(response){
    $("#theTextArea").val(response); // where theTextArea is the ID of the textarea you want to put the data into.
});