0

I am trying to run below given code.It is working properly in IE browser but in other browser it is not working.

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4 /jquery.min.js'></script>

<script type="text/javascript">
$(document).ready(function(){
 $("#button1").click(function(){

    alert("1");     
        $.ajax({url:"file://///30.170.12.75/Shared/a.html"
        ,success:function(result) {$("#div1").html(result);}
        ,error: function(result) {alert("2");}
         });
  });
});
<script>

As i know jquery api provide us browser compatibility also but it is not working any of other browser.

Please Let me know the reason so that in future i will be taking care of them.

Java_Alert
  • 1,159
  • 6
  • 24
  • 50

1 Answers1

2

You're using file:// protocol and most browsers don't allow access to this if the source document is not server with the file:// protocol.

For ajax requests, most browsers will block the requests if the destination protocol is a file:// protocol. However, IE doesn't seems to behave like this and still allow the request to finish.

If you're using Chrome, you can change this by starting Chrome with a --allow-file-access-from-files flag.

(Is it really to be file:// and not http:// or other things?)

Licson
  • 2,231
  • 18
  • 26
  • I am directly accessing this "file://///30.170.12.75/Shared/a.html" url in FF and chrome working properly. – Java_Alert Mar 01 '13 at 13:08
  • 2
    Directly accessing a URL does not invoke the security limits enforced on JavaScript based attempts to access URLs. Most browsers won't allow access to different files even if the source document is served using `file:`. Use HTTP. – Quentin Mar 01 '13 at 13:30
  • 1
    @Java_Alert — IE isn't "most browsers". Different browsers implement different levels of protection for local files. IE allowing it explains why I keep getting phishing/trojan emails with HTML attachments they want me to open though. – Quentin Mar 04 '13 at 09:24
  • @Quentin Http is not working at all. I have already checked it. – Java_Alert Mar 04 '13 at 09:26
  • From a file URI, most browsers will block access to other file URIs and HTTP URIs. Load the original document over HTTP and use Ajax to request resources from the same origin as the original document. – Quentin Mar 04 '13 at 09:47