3

I have a Java service running on my main OS(Windows7 Pro.) and I can access it throu broswer at http://localhost:8080/.... It returns valid JSON reposnse.

I also have a SPA written in angularjs, that is located on my virtual machine where I created a Virtual-Host for it(for more comfortable access). Virtual machine OS is Ubuntu Server 12.04.X LTS.

Since those environments are in network terms separated from each other, I was trying to access Java service through my local machine IP address X.X.X.X:8080(pinging through terminal did send response). Since normal $http usage didn't work I tried solution suggested in this answer How to load a cross-domain JSON with $http in angularjs 1.0.8 as it does with 1.2.0, but either it is too old or something else is wrong, but when I try to do same it just doesn't work. In console I see only following

XMLHttpRequest cannot load http://X.X.X.X:8080/library/api/books. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://dev.xxx' is therefore not allowed access.

Community
  • 1
  • 1
Eugene
  • 4,352
  • 8
  • 55
  • 79
  • 3
    You need to enable cross origin resource sharing (CORS) on your java service. – Anthony Chu Feb 13 '14 at 21:59
  • Tried to enable it as it is explained here http://cxf.apache.org/docs/jax-rs-cors.html, but didn't help in anyway. – Eugene Feb 13 '14 at 23:28
  • 1
    Make sure you have added the CORS interceptor to your jax-rs server. CORS works if you follow the Apache CXF instructions to the letter. – Geoff Genz Feb 14 '14 at 00:11

1 Answers1

0

Thanks everyone for their input. Can't say was my solution correct or not, but I got result I was expection.

I combined info found here Filter requests for CORS with answer from this question Java / Jetty: How to Add Filter to Embedded Jetty. I wasn't sure how to add a filter, that would listen for request and set response headers accordingly.

Hope that solution will help to those who might come in contact with same situation.

Community
  • 1
  • 1
Eugene
  • 4,352
  • 8
  • 55
  • 79