0

I am trying to get into jquery/ajax and I can't even believe I can't get past this first test. I'm following an example I found at The Jquery API site and I followed it just about to a T.

I created a local folder on the desktop, and added 2 files.

index.html

and

list1.html.


Index.html:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

<body>

<div id="stage">
</div>

<script>
$( "#stage" ).load( "list1.html" );
</script>

</body>

</html>

list1.html

<div id="list">
<li>Test</li>
<li>Foo</li>
<li>Bar</li>
</div>

I was trying for like 15 minutes to run index.html in chrome and nothing displayed (like the jquery wasn't loading correctly). Out of pure curiosity I opened it with firefox and it displayed as expected.. something like this

  • Test
  • Foo
  • Bar

So is this a browser issue? Why does Chrome and IE not show this loaded list, but firefox does? I can't figure out if it's my code or the environment which is infuriating when trying to learn.

Christopher
  • 790
  • 12
  • 30
  • Try wrapping the load code in `$(document.ready)`. Could be that jQuery isn't loaded at the time you're trying to run the function. – sg.cc Oct 07 '15 at 15:15
  • Do you know that the jQuery didn't load correctly? Did you check the chrome dev tools console and network tab? It loaded up just fine for me... – Jacob Petersen Oct 07 '15 at 15:19
  • Yes, @PeteTalksWeb it failed to load due to the error "Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.". Or in other words, chrome doesn't like opening files local to a machine. I wonder why it opened for you though... – Christopher Oct 07 '15 at 15:26
  • I'm running mine on a local server. So it's at http://localhost, not a file directory. – Jacob Petersen Oct 07 '15 at 15:30

2 Answers2

2

Try launching chrome / chromium with --allow-file-access-from-files flag set

See How do I make the Google Chrome flag "--allow-file-access-from-files" permanent?

guest271314
  • 1
  • 15
  • 104
  • 177
  • This works as a solution for Chrome... for now I will just do all my local testing and playing in firefox though! So much easier haha – Christopher Oct 07 '15 at 15:31
1

Try

<script>
    $(function(){
       $("#stage").load("list1.html");
    });
</script>

If still not works, check the Network section in the Developer Tools of your browser and see if there are any HTTP or Security errors.

Noldor
  • 1,122
  • 9
  • 22
  • Yes! Thank you... it wasn't that the Jquery function was the issue, it was that chrome was blocking the xml request because it was coming from a local directory! – Christopher Oct 07 '15 at 15:22