0

Im trying to enable CORS in my jersey web server. I read this page. So i created my filter

import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

@Provider
public class CORSFilter implements ContainerResponseFilter {


    @Override
    public void filter(final ContainerRequestContext requestContext,
            final ContainerResponseContext responseContext) throws IOException {

        final MultivaluedMap<String, Object> headers = responseContext
                .getHeaders();

        headers.add("Access-Control-Allow-Origin", "*");
        headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
        headers.add("Access-Control-Allow-Credentials", "true");
        headers.add("Access-Control-Allow-Headers",
                "X-Requested-With, Content-Type, X-Codingpedia, ");

    }

}

This is my jQuery test

    $.ajax({
        type: 'GET',
        url: myURL,
        crossDomain: true,
        dataType: 'jsonp',
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data.statusText);
            console.log(data.name);
        },
        headers: {
            "Authorization": "Basic " + btoa('login:pass'),
            "Accept": "application/json"
        }
    });

When i run this js it keep asking me the login and password and, even if i type right always return 401 unauthorized.

  • BTW im using apache shiro to authentication. When i was testing the webservices through postman i send basic authentication and works, now that im sending through the javascript above the browser keep requesting the login and pass. – Tiago Wanke Marques Aug 14 '14 at 19:16
  • It was my mistake, it wasnt authenticating because of a database connection problem, still i dont know why the browser keep requesting the login and password once i have already passed through the jQuery request. – Tiago Wanke Marques Aug 14 '14 at 23:08

1 Answers1

0

Solve it, i found out with this post. Just neede to change from

dataType: 'jsonp',

to

dataType: 'json',

Since jsonp, ignores the ajax request header.

Community
  • 1
  • 1