Ajax 函数仅适用于alert()
我试图让 AJAX 读取一个文本文件(可以工作),但如果我在函数中有一个 alert()
,它只会显示 responseText
不想要)。
有没有办法让它在没有 alert()
的情况下显示 responseText
?这是我当前的代码。
<script type="text/javascript">
function load(){
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "current.txt", true);
txtFile.send(null);
document.write(txtFile.responseText);
}
window.onload = load;
</script>
I'm trying to get AJAX to read a text file(which works) but it will only display the responseText
if I have an alert()
in the function(which I don't want).
Is there a way to get it to displa the responseText
without an alert()
? This is my current code.
<script type="text/javascript">
function load(){
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "current.txt", true);
txtFile.send(null);
document.write(txtFile.responseText);
}
window.onload = load;
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 .open 方法更改为 false,以便不使用异步。如果为 true,则将回调处理程序分配给
onreadystatechange
属性以确定调用何时完成。添加警报会增加足够的等待时间以等待文件返回,因此它可以正常工作。来源:http://msdn.microsoft.com/ en-us/library/ms536648(v=vs.85).aspx
Change the .open method to false so as not use async. If true, assign a callback handler to the
onreadystatechange
property to determine when the call has completed. Adding a alert was adding a sufficient wait for the file to return so it worked.Source: http://msdn.microsoft.com/en-us/library/ms536648(v=vs.85).aspx