You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何复现w3schools的jQuery.load示例时始终无法加载内容?

jQuery load() 加载本地文件始终返回error,无法加载内容

我尝试用jQuery的load()方法将本地txt文件的内容加载到HTML文件中,完全按照示例配置(demo_test.txt与调用它的testjquery.html在同一目录),但脚本每次返回statusTxt = error,内容无法加载。

我能正常运行其他所有jQuery示例,但唯独这个load函数无法工作。试过在Chrome、Firefox中运行,均返回错误状态,IE还额外提示“access denied”。

相关代码

demo_test.txt 内容:

<h2>jQuery and AJAX is FUN!!!</h2>
<p id='p1'>This is some text in a paragraph.</p>

testjquery.html 内容:

<!DOCTYPE html>
<html>
  <head>
    <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script>
    <script>
      $(document).ready(function () {
        $("button").click(function () {
          $("#div1").load(
            "demo_test.txt",
            function (responseTxt, statusTxt, xhr) {
              if (statusTxt == "success")
                alert("External content loaded successfully!");
              if (statusTxt == "error")
                alert("Error: " + xhr.status + ": " + xhr.statusText);
            }
          );
        });
      });
    </script>
  </head>
  <body>
    <div id='div1'><h2>Let jQuery AJAX Change This Text</h2></div>

    <button>Get External Content</button>
  </body>
</html>

内容的提问来源于stack exchange,提问作者boda17

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 11:09:55