我正在尝试将我的WebStorm应用程序中的请求发送到我的后端应用程序,这两个应用程序都在不同的端口,我正在前端和Java Spring 4.2.5与AngularJS合作后端。我尝试了各种事情,例如添加CORS过滤器,CORS注释,但是,在执行此错误之后,我是
XMLHttpRequest cannot load http://localhost:8081/appUser. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 404.
AngularJS控制器看起来像
registerController.factory('registerFactory', function(){
return {
getClassification:function($http,tempString){
//TODO add URL
var url = "http://localhost:8081/example?eg="+tempString
return $http({
method: 'POST',
url: url
});
}
}
});
弹簧控制器看起来像
@RequestMapping(value = "/example", method = RequestMethod.POST)
@ResponseBody
public String postAppUser(@RequestParam String eg) throws JsonProcessingException {
return "ok yes ";
}
我尝试添加CORS过滤器,但似乎无效。不确定该应用程序是否拾取了它。我的意思是,如果它在配置路径中。
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
@Component
public class CORSFilter implements Filter {
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", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
}
web.xml看起来像
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>cors</filter-name>
<filter-class>com.myApp.security.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
这就是HTTP调用的样子。它不包含允许的访问标头
Request URL:http://localhost:8081/appUser
Request Method:GET
Status Code:404 Not Found
Remote Address:[::1]:8081
Response Headers
view source
Content-Length:965
Content-Type:text/html;charset=utf-8
Date:Sun, 19 Jun 2016 03:43:40 GMT
Server:Apache-Coyote/1.1
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:localhost:8081
Origin:http://localhost:63342
如果有人可以指导我,我真的很感激。自从过去5天以来,我被卡住了,似乎无法正常工作。如果您可以指向博客/教程/视频,我可以参考
Spring IT自我提供功能以启用CORS。请参阅下面的示例。
https://spring.io/guides/gs/rest-service-cors/
启用cors,用
注释您的请求方法@CrossOrigin(origins = "http://localhost:63342")
看看CORS的春季文档
update
我在应用程序中也有相同的问题,在请求方法中删除了CrossOrigin
注释,并在Angular Service JS
delete $http.defaults.headers.common['X-Requested-With'];
和
return $http({
method: 'POST',
url: '/example'/ + '?eg=' + tempString
});