gpt4 book ai didi

java - Java RESTful 服务的 Jquery 调用不工作 CORS

转载 作者:行者123 更新时间:2023-11-29 05:30:40 25 4
gpt4 key购买 nike

我在 Tomcat 7 上部署并运行了一项休息服务。我可以通过浏览器获取响应数据,但是当我通过 jQuery 尝试时,它显示错误。请检查快照。

@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) req;
HttpServletResponse httpRes = (HttpServletResponse) res;

//System.out.println("Client Ip is : "+ SecurityFilter.getClientIpAddr(httpReq));

String domain = new URL(httpReq.getRequestURL().toString()).getHost();
//System.out.println("Domain is " + domain);

// referrer
String referrer = httpReq.getHeader("referer");
//System.out.println("Referral URL is -" + referrer);

// Security Key
String securityKey = httpReq.getParameter("secKey");
//System.out.println("Security Key Parameter " + securityKey);

// Origin
// No Origin header present means this is not a cross-domain request
String clientOrigin = httpReq.getHeader("Origin");
//System.out.println("Origin of the Request " + clientOrigin);

// CORS implementation
httpRes.addHeader("Access-Control-Allow-Origin", "*");
httpRes.addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
// httpRes.addHeader("Access-Control-Allow-Origin", clientOrigin);

if ("OPTIONS".equalsIgnoreCase(httpReq.getMethod())) {
httpRes.addHeader("Access-Control-Allow-Credentials", "true");
}

ValidateClient vl = new ValidateClient();

String secKey = "fmg_seckey"; //"SEC_1";;
String clientUrl = "fmggroup.com";//"www.xyzclient1.com";

// if request is coming from third party. Referral will be null if all
// request is from same server.

if (referrer != null) {
clientUrl = referrer.split("/")[2];
secKey = securityKey;
}

//System.out.println("Security Key " + secKey);
//System.out.println("Domain Name for the client " + clientUrl);


if (vl.isValidClient(secKey, clientUrl)) {
// httpReq.getRequestDispatcher("/intellixservices/activetime").forward(httpReq,httpRes);
chain.doFilter(httpReq, httpRes);
} else {

httpRes.sendError(HttpServletResponse.SC_UNAUTHORIZED,"Not Authorised");
// httpReq.getRequestDispatcher("/intellixservices/exception").forward(httpReq,
// httpRes);

}

}

在资源类中:

// CORS implementation
private String corsHeaders;

private Response makeCORS(ResponseBuilder responseBuilder, String returnMethod) {
ResponseBuilder rb = responseBuilder.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");

if (!"".equals(returnMethod)) {
rb.header("Access-Control-Allow-Headers", returnMethod);
}
return rb.build();
}

private Response makeCORS(ResponseBuilder responseBuilder) {
return makeCORS(responseBuilder, corsHeaders);
}

@OPTIONS
public Response getEmployee(@HeaderParam("Access-Control-Request-Headers") String request) {
corsHeaders = request;
return makeCORS(Response.ok(), request);
}

然后返回

Response response = Response.status(404).build();

if (mf != null) {
response = makeCORS(Response.status(200).entity(mf));
} else {
response = makeCORS(Response.status(500));
}

return response;

我无法解决它。请提出建议。

enter image description here

这是我的 jQuery 调用:-

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="application/javascript">
(function($) {
var url = 'http://localhost:8888/IntellixWebApi/intellixservices/dnareport/MTk3MC0wOS0wNFQwNjowNTowMA==/NDEuNTU4MTUyNSA=/LTczLjA1MTQ5NjYg/QW1lcmljYS9OZXdfWW9yaw==/MTpBOjB8QToxOjF8MToxOjE=/json';

$.ajax({
type: 'GET',
url: url,
async: true,
contentType: 'application/json',
success: function(response) {
alert("success");
},
error: function(xhr) {
alert('Error! Status = ' + xhr.status + " Message = " + xhr.statusText);
}
});
})(jQuery);
</script>
</head>
<body>
<!-- we will add our HTML content here -->
</body>
</html>

最佳答案

经过大量谷歌搜索后,我以任何方式解决了这个问题。我在 web.xml 中添加了 CORS 过滤器和 java 实用程序 jar 过滤器以及一些配置更改。这些链接非常有用

Link 1

Link 2

感谢大家的大力帮助。

关于java - Java RESTful 服务的 Jquery 调用不工作 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21178823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com