$http error with Ionic and spring

Asked

Viewed 79 times

2

have an API with spring framework follows 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 $GET request from angular js on 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

1 answer

2


I found the bug the problem is CROS to troubleshoot 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() {}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.