admin管理员组文章数量:1414892
i have a huge problem with CORS on my server. My server is running on Tomcat on localhost. The server i implemented has a couple of RestFul resources. For example:
@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;
}
}
The Ressource is working. Now i want to access to the Ressource via JavaScript from a different Website. When i try to do this, i get a Error message:
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.
I know i have add to my Server the Permission for CORS. I do it this way:
web.xml:
<filter>
<filter-name>CORS</filter-name>
<filter-class>.thetransactionpany.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>
To access with 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>
In addition i have added these 2 jars.
cors-filter-1.9.2.jar
java-property-utils-1.9.jar
I read so many examples and explanations on the Internet, but nothing seems to work and i don't know what i am doing wrong.
i have a huge problem with CORS on my server. My server is running on Tomcat on localhost. The server i implemented has a couple of RestFul resources. For example:
@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;
}
}
The Ressource is working. Now i want to access to the Ressource via JavaScript from a different Website. When i try to do this, i get a Error message:
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.
I know i have add to my Server the Permission for CORS. I do it this way:
web.xml:
<filter>
<filter-name>CORS</filter-name>
<filter-class>.thetransactionpany.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>
To access with 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>
In addition i have added these 2 jars.
cors-filter-1.9.2.jar
java-property-utils-1.9.jar
I read so many examples and explanations on the Internet, but nothing seems to work and i don't know what i am doing wrong.
Share Improve this question edited Jun 28, 2014 at 15:26 Sam 7,40816 gold badges48 silver badges68 bronze badges asked Apr 28, 2014 at 13:51 A. JanikA. Janik 491 silver badge4 bronze badges1 Answer
Reset to default 3I just succeed in passing CORS with tomcat and angular.js.
There's something you may need to configure:
Change cors.supportedHeaders to this:
<init-param> <param-name>cors.supportedHeaders</param-name> <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value> </init-param>
Make sure your filter is before any other filters.
Try to change contentType to this:
contentType: "application/x-www-form-urlencoded; charset=utf-8"
Try it man.
本文标签: httpTomcat quotAccessControlAllowOriginquot and JavaScriptStack Overflow
版权声明:本文标题:http - Tomcat "Access-Control-Allow-Origin" and JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745173668a2646126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论