javascript 通过 AJAX 加载文本文件会出现受限 URI 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21855355/
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
Loading a text file through AJAX gives restricted URI error
提问by Neeraj Verma
I mentioned i read the suggested link ...and Could not able to understand the suggestion .."Use Greasemonkey to modify Pages and start writing some javascript to modify a web page
我提到我阅读了建议的链接......并且无法理解该建议......”使用 Greasemonkey 修改页面并开始编写一些 javascript 来修改网页
I am loading a text file with $.ajax
. When running the code on Firefox, I get the following error:
我正在加载一个带有$.ajax
. 在 Firefox 上运行代码时,出现以下错误:
Error: ["Access to restricted URI denied" code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)" location: "<unknown>"]
错误:[“访问受限 URI 被拒绝”代码:“1012”nsresult:“0x805303f4(NS_ERROR_DOM_BAD_URI)”位置:“<unknown>”]
Here's my code:
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$("button").click(function () {
$.ajax({ url: "demo_test.txt",
success: function (result) {
$("#div1").html(result);
},
error: function (abc) {
alert(abc.statusText);
},
cache:false
});
return false;
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>
</body>
</html>
I've already read the following questions:
我已经阅读了以下问题:
- firefox reading web page from local JS file -- access to restricted URI denied, code: 1012, nsresult: NS_ERROR_DOM_BAD_URI
- Error: [Exception... "Access to restricted URI denied" .... while calling $.ajax method
- firefox 从本地 JS 文件读取网页——访问受限 URI 被拒绝,代码:1012,nsresult:NS_ERROR_DOM_BAD_URI
- 错误:[异常...“访问受限 URI 被拒绝”.... 调用 $.ajax 方法时
It was suggested that file system should not be used, so changed the URL to http://demo_test.txt, but that did not solve the issue.
建议不要使用文件系统,因此将 URL 更改为http://demo_test.txt,但这并没有解决问题。
I also heard that it might be because of a cross domain issue. If so, what exactly is meant by that, and how should I solve the problem?
我也听说这可能是因为跨域问题。如果是这样,那究竟是什么意思,我应该如何解决这个问题?
回答by epascarello
Browser security prevents the code from running. You are better off running a local server such as IIS or Apache.
浏览器安全性阻止代码运行。最好运行本地服务器,例如 IIS 或 Apache。
You can change your browser to run local content by changing a browser config
您可以通过更改浏览器配置来更改浏览器以运行本地内容
Firefox
火狐
- Go to about:config
- Find security.fileuri.strict_origin_policy parameter
- Set it to false
- 转到关于:配置
- 查找 security.fileuri.strict_origin_policy 参数
- 将其设置为假
回答by Neeraj Verma
I finally seems to Get it working . Here is working Script
我终于似乎让它工作了。这是工作脚本
$("button").click(function(){
$.ajax({url:"http://localhost/demo_test.txt",success:function(result){
$("#div1").html(result);
}});
});
Workaround : put the html file and text file on local server (IIS) New Site .
解决方法:将 html 文件和文本文件放在本地服务器 (IIS) 新站点上。