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.

3
  • 3
    You need to enable cross origin resource sharing (CORS) on your java service. Commented Feb 13, 2014 at 21:59
  • Tried to enable it as it is explained here cxf.apache.org/docs/jax-rs-cors.html, but didn't help in anyway. Commented Feb 13, 2014 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. Commented Feb 14, 2014 at 0:11

1 Answer 1

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.

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

Comments

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.