error in $ http with ionic and spring

2

I have an API with spring framework follow the method below:

@RequestMapping(value = "/users", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<List<Usuario>> listUsers () {
    return new ResponseEntity<List<Usuario>>(usuarioService.listaDeUsuarios(), HttpStatus.OK);
}

and the request $ GET of angular js no ionic:

  $http.get('http://localhost:8080/users').then(function(resp) {
    console.log('Success', resp.data);
  }, function(err) {
    console.error('ERR', err.status);
  });

Exit: ERR: 0

    
asked by anonymous 08.02.2016 / 15:30

1 answer

2

I found the error the problem is CROS to solve creates a configuration class:

package br.com.example.config;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

@Component
public class SimpleCORSFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}
}
    
08.02.2016 / 15:45