Ajax 函数仅适用于alert()

发布于 2024-12-28 10:07:43 字数 526 浏览 1 评论 0原文

我试图让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚你发端 2025-01-04 10:07:43

将 .open 方法更改为 false,以便不使用异步。如果为 true,则将回调处理程序分配给 onreadystatechange 属性以确定调用何时完成。添加警报会增加足够的等待时间以等待文件返回,因此它可以正常工作。

txtFile.open("GET", "current.txt", false);

来源: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.

txtFile.open("GET", "current.txt", false);

Source: http://msdn.microsoft.com/en-us/library/ms536648(v=vs.85).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文