8

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?

Community
  • 1
  • 1
Neeraj Verma
  • 253
  • 2
  • 10
  • 21
  • 1
    Are you running off the filr protocol...aka `c:\\test\foo.html` https://github.com/mrdoob/three.js/wiki/How-to-run-things-locally – epascarello Feb 18 '14 at 13:50
  • I guess i am not . Based on your link , i did following : 1. changed url to "D:\\demo_test.txt" 2.Change local files security policy in Firefox . But still it giving me same error – Neeraj Verma Feb 18 '14 at 14:02
  • Are you running off a local server....aka `http://localhost` or you just clicking on a file and it opens in a browser. – epascarello Feb 18 '14 at 14:04
  • i click on HTML file . it opens in FireFox with address "file:///C:/Users/Administrator/Desktop/New%20folder/my_html.html" – Neeraj Verma Feb 18 '14 at 14:06
  • 1
    That you are running on the local file protocol. You need to enable firefox to let it access files like the link I posted in the first comment tells you. You really should run IIS or Apache locally and not have to deal with this. – epascarello Feb 18 '14 at 14:07
  • possible duplicate of [firefox reading web page from local JS file -- access to restricted URI denied, code: 1012, nsresult: NS\_ERROR\_DOM\_BAD\_URI](http://stackoverflow.com/questions/2666839/firefox-reading-web-page-from-local-js-file-access-to-restricted-uri-denied) – Qantas 94 Heavy Feb 22 '14 at 12:15
  • I mentioned i read that link ...its answer "If you only want to modify some pages, you can also look into Greasemonkey, and start writing some javascript to modify a web page without bothering to learn how to make an extension." .... i could not understand that unfortunatelly – Neeraj Verma Feb 22 '14 at 12:24

2 Answers2

16

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
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • I did that setting already .Now i put my html file and text file in default Web site of IIS(it is running) and browse it again : New url is "http://localhost:2020/my_html.html"......but there is still Exception : [Exception... "" nsresult: "0x805e0006 ()" location: "JS frame :: http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js :: .send :: line 6" data: no] – Neeraj Verma Feb 18 '14 at 14:38
  • your suggestion helped – Neeraj Verma Feb 19 '14 at 07:00
  • It helped me too, but do you know if it is a FF particularity ? – jeum Nov 27 '15 at 13:16
1

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 .

Neeraj Verma
  • 253
  • 2
  • 10
  • 21