0

Here is my code:

<script type="text/javascript">
   $(".link").click(function(){
   var id = this.id;

   if(id == 'b1'){
      $( "#conteudo" ).load( "teste.html", function(){
      alert('Carregou de boa !');
   });
   }      
   });
</script>

All my pages are in localhost, I'm doing this just for study.
And this is the error that Chrome is giving me when I press the B1 button:

UPDATE Full Error:

OPTIONS file:///E:/mypath/folder/page.html No 'Access-Control-Allow-Origin'   
header is present on the requested resource. Origin 'null' is therefore not allowed access.

3 Answers 3

1

If you're doing this locally, make sure you are visiting your page over http and not the file protocol.

Also, just to make sure you're going to same origin, you can add // to the link like so:

$( "#conteudo" ).load( "//teste.html", function(){

You can pick up a local virtual machine webserver (Virtual Box) here: http://virtualboxes.org/images/

Sign up to request clarification or add additional context in comments.

7 Comments

Now I'm getting only 1 error, this one: *XMLHttpRequest cannot load file://teste.html/. Cross origin requests are only supported for HTTP. *
Indeed, you're not going to be able to do XMLHttpRequest over file:/// protocol
So, I can not use it on localhost ?! D:
You can use it on localhost, but only if you go to it over http://, not file:///. You'll need to have a local webserver running, or a virtualmachine with a webserver built in, in order to run it. I suggested a virtual machine, because you can get one pre-configured. Otherwise you'll have to configure your local one.
It's because file protocol is an entirely different protocol than Http. Your browser is able to interpret the markup from the file protocol, but it's not truly http, which XMLHttpRequest must use (it's in the name ;)). As well, same origin policy sees the different protocols as different origins and therefore can't do anything with an XMLHttpRequest that's requested by a page on the file:/// protocol.
|
0

Chrome only does XMLHttpRequests over http(s). Use http://localhost instead of file://localhost

1 Comment

Really man? It's a really big path D: I'll do this for as a job later, how may I do that when I update this to the server? Like: public_html/ajax/page.html ?
0

If you have python installed run SimpleHTTPServer with:

python -m SimpleHTTPServer

If you do this in the same dir where your web files are located you will have a webserver running in a very easy way

2 Comments

I don't think I have it'-'
if you use windows you could install cygwin, gitbash or something similar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.