我正在尝试从另一个域调用我的struts2动作调用。ajax调用的是localhost:8080,而进行ajax调用的是另一个localhost:3000.。我尝试将sample.json文件放在struts2项目文件夹的web文件夹下,我可以对sample.json文件执行ajax调用。但问题是,当我试图请求我的动作类时,我会得到一个错误,上面写着:
XMLHttpRequest cannot load http://localhost:8080/workforce/loginAction. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.我的问题是,我可以在同一个项目http://localhost:8080/sample.json下对示例json文件执行ajax调用,但不能使用我的struts2操作类?
Struts2行动:
package com.workforce.actions;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.workforce.models.UserModel;
public class LoginAction extends ActionSupport implements ModelDriven<UserModel>{
/**
*
*/
private static final long serialVersionUID = 1L;
private UserModel user = new UserModel();
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
user.setEmail("sampleemail");
user.setPassword("12345");
System.out.println(user.getEmail());
System.out.println(user.getPassword());
return SUCCESS;
}
@Override
public UserModel getModel() {
// TODO Auto-generated method stub
return user;
}
}struts2 xml
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default, json-default">
<interceptors>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="json" />
</interceptor-stack>
</interceptors>
<action name="loginAction" class="com.workforce.actions.LoginAction">
<interceptor-ref name="myStack" />
<result name="success" type="json"/>
<result name="input" type="json">
<param name="statusCode">500</param>
<param name="errorCode">500</param>
</result>
</action>
</package>
web.xml
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.preflight.maxage</param-name>
<param-value>10</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>Ajax调用
var request = {
url: "http://localhost:8080/workforce/loginAction",
method: "POST",
headers:{
"Content-Type": "application/json"
},
data: {
user: self.user
}
};
$http(request).then(function(response){
console.log(response);
});更新
我尝试在struts2中注释我的web.xml配置并创建一个样例servlet。而且起作用了!我只想知道如何用CorsFilter配置Struts2 Filter
发布于 2016-07-06 11:42:26
过了一段时间,我把CorsFilter放在第一位,而不是Struts2 Filter,解决了这个问题。现在起作用了。谢谢。
发布于 2018-07-03 09:59:22
U可以在压杆中检查CORS的解。这是一个简单的演示应用程序,解决了struts2中的CORS问题。
https://stackoverflow.com/questions/38221689
复制相似问题