Hi I'm working on a REST api in java using Glassfish 4.1.1 as server and java EE 7 and as front end I'm using AngularJS also on netbeans.
I was following this simple tutorial
https://www.youtube.com/watch?v=2B3qL7XtKnE
and created the back succesfully, but when I try to use a GET call from the front end I get the common error Access-Control-Allow-Origin. In the video the girl uses the cross-origin share filter template that comes with netbeans and fixes this issue.
package entities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
@Provider
public class awdawdCrossOriginResourceSharingFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext response) {
response.getHeaders().putSingle("Access-Control-Allow-Origin", "*");
response.getHeaders().putSingle("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE");
response.getHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type");
}
}
But I am still getting the same error. Seen many guides and tutorials but cant fix it.
Note: I am a total beginner in java, netbeans, etc. So any piece of information you can give me it will help.
Thanks
Here is the output from the Browser Log:
XMLHttpRequest cannot load http://localhost:8080/AngularBackEnd/rs/customer. Origin http://localhost:8383 is not allowed by Access-Control-Allow-Origin. (09:09:12:047 | error, javascript) at app/index.html
The call from angular:
empService.factory('Emps', function($resource){
return $resource('http://localhost:8080/AngularBackEnd/rs/customer', {}, {
findAll:{method:'GET', isArray:true}
})
});
Thanks again.
EDITED
Now I used https://github.com/ebay/cors-filter and followed his guide. Adding the web.xml
CORS Filter
org.ebaysf.web.cors.CORSFilter
CORS Filter
/*
30
To test I ran the command :
curl -D -"http://localhost:8080/BackEndTest/webresources/service.customer"
And this gave me:
Server: GlassFish Server Open Source Edition 4.1.1
X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1.1 Java/Oracle Corporation/1.8)
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: OPTIONS, GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type
Content-Type: application/xml
Date: Fri, 09 Sep 2016 17:50:00 GMT
Content-Length: 6875
As it show it gives me access-control needed but I still get the same error as before.
No comments:
Post a Comment