jQuery .load() 在 Chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2990518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:41:52  来源:igfitidea点击:

jQuery .load() not working in Chrome

jquerygoogle-chrome

提问by Skilldrick

.load()was working fine, but for some reason it's not in Chrome now (presumably some kind of update). I'm on Chrome 5.0.375.55. I've isolated the problem to this:

.load()工作正常,但由于某种原因,它现在不在 Chrome 中(大概是某种更新)。我在 Chrome 5.0.375.55 上。我已将此问题隔离为:

index.htm

索引.htm

<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function () {
          $('#reader').load('test.htm');
      });
    </script>

  </head>
  <body>
    <div id='reader'>Loading ...</div>
  </body>
</html>

test.htm

测试.htm

<h1>Loaded successfully</h1>

This works on Firefox and IE, but on Chrome it replaces the contents of the #readerdiv with nothing.

这适用于 Firefox 和 IE,但在 Chrome 上它会用空替换#readerdiv的内容。

Edit:I should add that I'm running this locally, and it's never going to be deployed on a web server.

编辑:我应该补充一点,我在本地运行它,它永远不会部署在网络服务器上。

Edit2:I've tried .load('file:///C:/path/to/test.htm')which works in FF but not Chrome.

Edit2:我试过.load('file:///C:/path/to/test.htm')在 FF 中有效但在 Chrome 中无效。

采纳答案by Ken Redler

It's the "same origin policy" as interpreted by Chrome (or a bug with the same effect). Try running Chrome with --allow-file-access-from-files.

这是 Chrome 解释的“同源策略”(或具有相同效果的错误)。尝试使用--allow-file-access-from-files.

回答by Duc Manh Nguyen

I've the same problem, but you may find it works if you upload it to a web server. I've tested it and it done when i upload test page to server.

我有同样的问题,但如果你将它上传到网络服务器,你可能会发现它有效。我已经测试过了,当我将测试页面上传到服务器时它就完成了。

回答by JwC

Neither --disable-web-securitynor --allow-file-access-from-filesworked for me, although I found that using '127.0.0.1' instead of 'localhost' solved the problem.

虽然我发现使用 '127.0.0.1' 而不是 'localhost' 解决了问题,但对我来说既不工作--disable-web-security也不--allow-file-access-from-files适用。