3

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

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<filter>
    <filter-name>CORS Filter</filter-name>
    <filter-class>org.ebaysf.web.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CORS Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
</web-app>

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.

VictorS
  • 41
  • 1
  • 4

1 Answers1

0

The client has to send the Origin header.

See How does Access-Control-Allow-Origin header work? for more info and see How to send a header using a HTTP request through a curl call? for how to set the header via curl

In your case, you should be able to do something like this: curl --header "Origin: http://localhost" -D -"http://localhost:8080/BackEndTest/webresources/service.customer"

Community
  • 1
  • 1
Brian Pipa
  • 808
  • 9
  • 23