简体   繁体   中英

Error with AJAX, requesting a file from my local machine

I'm trying to use AJAX to load a section of html into my site, but i'm getting this error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

I've seen this error before but only when trying to request files on a different server. I'm loading a local file in the same directory as the page that's requesting it, so i'm not sure why i'm receiving this error.

Here's the javascript i'm using to request it

$('#page').load('homepage.html')

How are you loading "homepage/html"?

The error you are getting means either the protocol is different

ie: you are using

file://foo/bar/whatever_has_your_js_script.html

but you really want

 http://...

OR the host is different. Sound like this is not the case

In my experience, for work on a local machine, I "accidentally" open an html as a file, instead of by localhost/http. For example:

file://localhost/foo/mySpace/blog.html

would cause an error trying to get an ajax call, but

http://localhost/foo/mySpace/blog.html

might not. See: http://en.wikipedia.org/wiki/Same-origin_policy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM