0

I'm having a pretty weird problem with CORS on a webapp I'm trying to make I'm using Servlets (Tomcat8.0) for the backend. It's a school project, so I can't use a framework

A GET request to http://localhost:8080/FileBox/dashboard returns a JSON payload( plain json, not jsonp,which I could use, but its the same domain). I'm using ajax to make the XHR, but it's being blocked by chrome as CORSXHR blocked by chrome

Should this be happening, since I'm making the XHR from the same domain(host+port) 'localhost:8080/FileBox/dashboard.jsp' to 'localhost:8080/FileBox/dashboard'

Please, and thank you for the help!

  • I've looked at these, but they weren't particularly helpful: http://stackoverflow.com/questions/19966707/cors-error-on-same-domain?rq=1 http://stackoverflow.com/questions/25772001/xhr-cross-domain-error-on-the-same-domain-localhost – Kartik Mudgal Oct 31 '16 at 14:58

1 Answers1

1

You aren't making a request to http://localhost:8080/FileBox/dashboard. The error message says you are making a cross-origin request using an unsupported scheme and that http is a supported scheme.

Presumably you have made the two mistakes of:

Getting the URL wrong

You should be using a relative URL:

/FileBox/dashboard

but are trying to use an absolute URL:

http://localhost:8080/FileBox/dashboard

but have typed it wrong and are actually requesting

localhost:8080/FileBox/dashboard

Not loading the page over HTTP to start with

Possibly by double clicking the file in your system file manager, you have bypassed your HTTP server and are loading something like file:///c:/users/you/yourproject/index.html


Combined with the previous mistake, you end up trying to request file:///c:/users/you/yourproject/localhost:8080/FileBox/dashboard, with Ajax and get a security violation.


Solution

  1. Fix the URL to be a proper relative URL
  2. Point your browser at http://localhost:8080 instead of double clicking files in your file manager
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thank you for the response, I'm running a server, so It's not the second issue, I'll try and see If I can use a relative url – Kartik Mudgal Oct 31 '16 at 15:38
  • @DancesOnTheMoon — The second issue is that you appear to be running a server but aren't telling the browser to talk to it. – Quentin Oct 31 '16 at 15:38
  • It was the first issue, I was using an absolute url. I'm not aware why this should be a problem, since it's the same domain, but your answer helped me. Thanks – Kartik Mudgal Oct 31 '16 at 15:40