角度http响应代码始终为-1,表示错误40x

时间:2017-06-19 19:56:31

标签: java angularjs cors

我正在调用我的API。如果我提供正确的用户名和密码,它可以正常,但是,如果我提供了错误的凭据,则会失败,并且会失败,而401是预期的。我可以看到401在浏览器检查窗口中显示,但没有响应我从角度httpstatus获得的js。我总是以$http.get('url', config) // passing Auth headers .then(function successCallback(response) { //all good }, function errorCallback(response) { if (response.status == 401) { NotificationService.displayError('Unauthorized.') } else if (response.status == 403) { NotificationService.displayError('Access denied.') else { NotificationService.displayError('Failed to update user details.') } 为-1。我认为这与CORS过滤器有关,但我的CORS配置对我来说很好。

当响应为200时,同样的URL访问再次正常工作,但如果它是40x则返回-1。请指教。

我在浏览器中遇到的错误:

PATCH http://localhost:8080/users/1 401()

index.html#/ userpage / 1:1 XMLHttpRequest无法加载http://localhost:8080/users/1。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://localhost:63343”访问。响应具有HTTP状态代码401。

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, PATCH, PUT, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Content-Type, Authorization, X-Auth-Token, x-requested-with");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.addHeader("Access-Control-Expose-Headers", "X-Auth-Token");

    if (!request.getMethod().equals("OPTIONS")) {
        chain.doFilter(req, res);
    } else {
    }
}

});

Java CORS过滤器。

chrome

0 个答案:

没有答案