我正在尝试从另一个域调用我的struts2 action call。Struts 2位于localhost:8080上,而进行Ajax调用的是在localhost:3000上。我尝试将sample.json文件放在web文件夹下的struts 2项目文件夹中,我可以对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调用,但不能使用我的struts 2操作类?
Struts2 action:
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 call
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);
});
更新:
我尝试在web.xml中注释我的struts 2配置,并创建一个样例servlet。而且起作用了!我现在想知道如何用Struts 2过滤器配置CorsFilter......