问题描述
根据同源策略,SOP不应该适用于文件://协议,但为什么我的code不起作用?我正在从我的本地系统,此测试网页,我已经在的abc.txt的同一目录中的HTML页面。如果我改变网址 http://www.google.com/ ,这是行不通的了。我不明白为什么,谁能解释一下?
According to Same Origin Policy, SOP shouldn't apply to file:// protocol, but why my code does not work? I am running this testing page from my local system and I have abc.txt in the same directory as the html page. If I change URL to http://www.google.com/, it does not work too. I don't understand why, could anyone explain?
<!doctype html>
<html lang="us">
<head>
<meta charset="utf-8">
<title>jQuery UI Example Page</title>
<link href="css/sunny/jquery-ui-1.10.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery-ui-1.10.2.custom.js"></script>
<script>
$.support.cors = true;
$(function(){
$(".btn1").button().click(function(){
$.ajax({
url: "abc.txt"
}).done(function(result) {
alert("done");
$(".content").html(result);
}).fail(function() {
alert("error");
});
});
});
</script>
</head>
<body>
<button class="btn1">Click me!</button>
<div class="content"></div>
</body>
</html>
编辑:控制台打印如下:
Edited:The console printed as below:
XMLHtt prequest无法加载 file:///C:/Users/yc/Desktop/jquery%20ajax%20testing/jquery-ui-1.10.2.custom/jquery-ui-1.10.2.custom/abc.txt. 原产地空不受访问控制 - 允许 - 产地允许的。
*它不适用于Firefox的工作,IE浏览器了。
*It does not work for Firefox, IE too.
推荐答案
这是不是一个错误,这是一个安全功能,你不能/不会得到各地客户的计算机上。
This is not a bug, it's a security feature which you can't/won't get around on a client's computer.
在Chrome浏览器,你可以通过添加下面的标志在命令行上禁用
In chrome you can disable it by adding the following flag on the command line
- 禁用 - 网络安全
火狐可能有类似的东西,但我不知道它。这是仅用于开发目的是有用的,你可以不依赖于应用程序中的这种行为。
Firefox might have something similar but I don't know it. This is only useful for development purposes and you can't rely on this behaviour in your application.
您真的应该只使用一台服务器...
You should really just use a server ...
这篇关于无法阿贾克斯本地HTML页面中的本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!