Enable CORS Post Request from AngularJS to Jersey

前端 未结 4 1559
失恋的感觉
失恋的感觉 2020-12-05 20:17

I\'m attempting to post a JSON document from an AngularJS app to a Jersey REST service. The request fails, informing me that:

XMLHttpRequest cannot load http:/

4条回答
  •  死守一世寂寞
    2020-12-05 20:51

    I had faced similar CORS error while calling my Restful service (implemented in java - Jersey) from angularjs. To fix it I added Access-Control-Allow-Origin: * in response header. I added below :

    response.addHeader("Access-Control-Allow-Origin", "*"); 
    

    For more information you can check - http://enable-cors.org/server.html

    CORS error occurs typically when your angularjs code (web project) and webserivce code (server side project) are on different IP and port no.

    Your webservice implementation looks correct. So just to check, try running them on localhost on same port (eg. 8080). It should work there if all code is correct.

    In order to run them separately try adding Access-Control-Allow-Origin: * in webservice implementation as shown above.

    Hope this helps.

提交回复
热议问题