Jquery 中的 Ajax 不适用于本地文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17947971/
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
Ajax in Jquery does not work from local file
提问by Harrix
I created simple html file with simple ajax.
我用简单的ajax创建了简单的html文件。
index.html:
索引.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; Charset=UTF-8">
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<div id="content"></div>
<script>
function show()
{
$.ajax({
url: "2.html",
cache: false,
success: function(html){
$("#content").html(html);
}
});
}
$(document).ready(function(){
show();
setInterval('show()',1000);
});
</script>
</body>
</html>
File 2.htmllocated in the same directory as the file index.html. And contains for example:
文件2.html与文件index.html位于同一目录中。并包含例如:
<p>ssss hkl jh lkh <b>d1111</b></p>
When I run the index.htmlon the webserver, everything works. But if you run file index.htmlon the computer as a local file ajax not working. How to fix it?
当我在网络服务器上运行index.html时,一切正常。但是如果你在计算机上运行文件index.html作为本地文件 ajax 不起作用。如何解决?
回答by Quentin
Some browsers implement strong security measures to prevent downloaded webpages from accessing arbitrary files on the file system.
一些浏览器实施了强大的安全措施,以防止下载的网页访问文件系统上的任意文件。
Switch to a browser with weaker security (I think Firefox permits access to local files via XHR) or stop trying to run a website without HTTP.
切换到安全性较弱的浏览器(我认为 Firefox 允许通过 XHR 访问本地文件)或停止尝试在没有 HTTP 的情况下运行网站。
回答by Bijoy Anupam
This is a known problem with Chrome, if you are checking on it. Use XAMPP to run a local webserver, and test your ajax call.
如果您正在检查,这是 Chrome 的一个已知问题。使用 XAMPP 运行本地网络服务器,并测试您的 ajax 调用。
Check this ticket: https://code.google.com/p/chromium/issues/detail?id=40787
检查这张票:https: //code.google.com/p/chromium/issues/detail?id=40787