Problema com No 'Access-Control-Allow-Origin' header is present on the requested resource

Estou desenvolvendo um aplicativo com Ionic que vai acessar um backend remoto mas que por hora está na minha máquina. Ao tentar acessar o serviço(rest / json) de login dá o seguinte erro no browser:

No Access-Control-Allow-Origin`’ header is present on the requested resource.

Meu aplicativo é em html e AngularJS e meu backend é Java com Tomcat rodando local na porta 8080.

Alguém tem uma dica?

1 curtida

Coloca o código do seu serviço REST pra gente ver.

1 curtida
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.cxf.rs.security.cors.CrossOriginResourceSharing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Path("/login")
public class LoginService {
	
Logger logger = LoggerFactory.getLogger(LoginService.class);
EntityManagerFactory entityManagerFactory;

public LoginService(EntityManagerFactory entityManagerFactory)
{
	this.entityManagerFactory = entityManagerFactory;
}
	
//construtor default
public LoginService(){
	
}

@CrossOriginResourceSharing(allowAllOrigins = true)
	@POST
	@Path("/getLogin")
	@Produces({ MediaType.APPLICATION_JSON , MediaType.TEXT_PLAIN})
	public Response login(UsuarioFilter usuario) throws NoSuchAlgorithmException {
	
	Usuario u = new Usuario();
	String pwd = getPasswordHash(usuario.getSenha());
	
	try{
		
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		Query q = entityManager.createQuery("select u from Usuario u where u.login = :login and u.password = :password").setParameter("login", usuario.getUsuario()).setParameter("password", pwd);
		u = (Usuario) q.getSingleResult();
		
		//return Response.status(Status.OK).header("Access-Control-Allow-Headers, origin, content-type, accept, authorization, token, tokenFB, idFB", "*").build();
		return Response.status(Status.OK).entity(usuario).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Headers","origin, content-type, accept").header("Access-Control-Allow-Credentials", "true").header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD").header("Access-Control-Max-Age", "1209600").build();
		
	}catch(Exception e){
		e.printStackTrace();
		return Response.status(Status.BAD_REQUEST).entity("Usuário não encontrado").header("Access-Control-Allow-Origin", "*").build();
	}
			
}

Bah, estou com o mesmo problema,
Failed to load http://192.168.1.9:8080/FazendaWebService/webresources/fazenda/Produto/list: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8100’ is therefore not allowed access.