Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

javascript - Loading a text file through AJAX gives restricted URI error

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

I am loading a text file with $.ajax. When running the code on Firefox, I get the following error:

Error: ["Access to restricted URI denied" code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)" location: "<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:

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Browser security prevents the code from running. You are better off running a local server such as IIS or Apache.

You can change your browser to run local content by changing a browser config

Firefox

  1. Go to about:config
  2. Find security.fileuri.strict_origin_policy parameter
  3. Set it to false

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...