我想从另一个域调用我的struts2动作调用。 Struts2在本地主机:8080而谁将使ajax呼叫在另一个,localhost:3000。我试图把sample.json
文件放在web文件夹下的struts2项目文件夹中,我可以在sample.json
文件上执行ajax调用。但问题是,当我试图请求我的动作类,我得到它说的错误:Struts2 Ajax CORS
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.
我的问题是,我可以在同一项目中做样品JSON文件的AJAX调用http://localhost:8080/sample.json
但不是与我的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
http:// localhost:8080/workforce/sample.json此网址的作品,我可以检索其中的数据。 –
其实,我已经将它添加到了我的web.xml中,我只是没有放在那里,但我已经更新了我的答案以显示我的S2配置。我是否需要在动作课上实施CorsFilter?我只是把它放在我的web.xml中,没有任何内容。 –
你需要调试才能看到 - >你的CorsFilter被调用动作url吗?什么关于json文件的网址? –