gpt4 book ai didi

javascript - Tomcat "Access-Control-Allow-Origin"和 JavaScript

转载 作者:可可西里 更新时间:2023-11-01 15:28:15 25 4
gpt4 key购买 nike

我的服务器上的 CORS 有一个大问题。我的服务器在本地主机上的 Tomcat 上运行。我实现的服务器有几个 RestFul 资源。例如:

@Path("user")
public class UserService {
@GET
@Path("/{username}/{password}")
@Produces("text/plain")
public String checkLoginParameter(@PathParam("username") String username, @PathParam("password") String password) {
String tempUserName = "";
String tempPassword = "";
String bool = "";

EntityManager em = createEntityManager();
try{
User user = (User)em.createQuery("SELECT u FROM User u WHERE u.username='"+username+"' AND u.password='"+password+"'").getSingleResult();
tempUserName = user.getUsername();
tempPassword = user.getPassword();
}
catch(Exception e){}

if(username.equals(tempUserName) && !username.isEmpty() && password.equals(tempPassword) && !password.isEmpty()){
bool = "true";
}
else{
bool = "false";
}
return bool;
}
}

资源正在运行。现在我想通过 JavaScript 从不同的网站访问资源。当我尝试这样做时,我收到一条错误消息:

XMLHttpRequest cannot load http://localhost:8080/RSAppStore/user/poc/poc. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

我知道我已将 CORS 权限添加到我的服务器。我这样做:

web.xml:

<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>Set-Cookie</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

使用 JavaScript 访问:

<script type="text/javascript">
$(function(){
$.ajax({
type: "GET",
crossDomain: true,
url: "http://robosmart-appstore:robosmart@localhost:8080/RSAppStore/user/poc/poc",

contentType: "text/plain",
success: function( string ) {

console.log(string);


},
error:function () {
console.log("err");
}
});
});
</script>

此外,我还添加了这 2 个 jar 。

cors-filter-1.9.2.jar
java-property-utils-1.9.jar

我在 Internet 上阅读了很多示例和解释,但似乎没有任何效果,我不知道我做错了什么。

最佳答案

我刚刚通过 tomcat 和 angular.js 成功通过了 CORS。

您可能需要配置一些东西:

  1. cors.supportedHeaders 更改为:

    <init-param>
    <param-name>cors.supportedHeaders</param-name>
    <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>
    </init-param>
  2. 确保您的过滤器位于任何其他过滤器之前。

  3. 尝试将 contentType 更改为:

    contentType: "application/x-www-form-urlencoded; charset=utf-8"

试试吧,伙计。

关于javascript - Tomcat "Access-Control-Allow-Origin"和 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23342948/

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