1

The following works when I run off my web servers, however when I run off my local desktop (no local web server) I encounter an error.

below are the three files:

index.html

<html>
    <head>
        <script src="jquery-1.11.1.js"></script>
    </head>
    <body>
        <span>First or Second?</span> <input type="text" id="var"> <button type="button" onclick="load()">AJAX it</button>
        <div id="container">
        </div>
    </body>
    <script type="text/javascript">
        function load()
        {
            input = $('#var').val() + '.html';
            input = input.toLowerCase();
            if (input == 'first.html')
            {
                $( "#container" ).load( "first.html" );
            } else if (input =='second.html') {
                $( "#container" ).load( "second.html" );
            } else {
                $('#container').html('invalid input');
            }
        }
    </script>
</html>

first.html

first

second.html

second

However, when making either of the .load() calls, I get the following error:

XMLHttpRequest cannot load file:///C:/Users/XXXX/Desktop/XXXX/test/first.html. Received an invalid response. Origin 'null' is therefore not allowed access. 

Out of curiosity, why does this not work? The error message looks similar to some CORS errors. In fact, searching for more information to this leads to nothing but the slightly similar (but not identical) CORS error message when making a AJAX request to a different domain.

Matt
  • 1,490
  • 2
  • 17
  • 41
  • 2
    ajax calls can't be executed without a server (locally) as far as I know! take a look at this question: http://stackoverflow.com/questions/3397644/jquery-running-ajax-locally-without-a-webserver – Amin Jafari Aug 16 '14 at 05:03
  • If I enable CORS and make the AJAX request to a file on the internet, it does work even though index is a local file. So is it only that the requested file needs to be on a web server? – Matt Aug 16 '14 at 05:18
  • This question might be helpful: https://stackoverflow.com/questions/6923707/using-ajax-to-read-local-files – Ace Sotirovski Aug 16 '14 at 05:19
  • For security reasons, there are some limitations to what you can/can't do with local files, even when the web page is local. These limitations also tend to vary by browser. – jfriend00 Aug 16 '14 at 05:21
  • @Matt , whenever data has to be shared between web pages, a web server is required. So is the case with AJAX. – rahulmishra Aug 16 '14 at 05:28
  • By Mistake I have voted UP for Amin Jafari.... – Yunus Aslam Aug 16 '14 at 05:28

0 Answers0