Miro Hascic
Miro Hascic

Reputation: 143

chrome not allowing localhost origin

i have a tomcat server, in web xml i defined cors filter to accept all requests.

<filter>
   <filter-name>CorsFilter</filter-name>
   <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
   <init-param>
    <param-name>cors.allowed.origins</param-name>
    <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value>
    </init-param>
 </filter>
 <filter-mapping>
   <filter-name>CorsFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

In network inspector in chrome i get report that request arrived successfully, i even can see the response JSON. network inspector screenshot

But in console i still get this error message: Failed to load http://localhost:8080/refactor/repair: The 'Access-Control-Allow-Origin' header has a value 'null' that is not equal to the supplied origin. Origin 'null' is therefore not allowed access.

EDIT: just tried opening my web page in internet explorer and there it works fine

Upvotes: 1

Views: 520

Answers (1)

Kim Hogeling
Kim Hogeling

Reputation: 661

When a page requests an ajax request, but the page itself is not run on via a server, which means it does not have an http protocol, then it will also not have an origin. This is mandatory though for the server that receives the ajax requests to set Access-Control-Allow-Origin header.

By using a simple http server e.g. the one that python is shipped with, this could be solved with ease.

For windows users with python 2:

py -m SimpleHTTPServer <SOME_PORT>

For windows users with python 3:

py -m http.server <SOME_PORT>

For mac/linux users with python 2:

python -m SimpleHTTPServer <SOME_PORT>

For mac/linux users with python 3:

python -m http.server <SOME_PORT>
# or the binary might be python3:
python3 -m http.server <SOME_PORT>

Upvotes: 2

Related Questions