首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Struts2 Ajax CORS

Struts2 Ajax CORS
EN

Stack Overflow用户
提问于 2016-07-06 10:22:16
回答 2查看 4.6K关注 0票数 0

我正在尝试从另一个域调用我的struts2动作调用。ajax调用的是localhost:8080,而进行ajax调用的是另一个localhost:3000.。我尝试将sample.json文件放在struts2项目文件夹的web文件夹下,我可以对sample.json文件执行ajax调用。但问题是,当我试图请求我的动作类时,我会得到一个错误,上面写着:

代码语言:javascript
复制
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行动:

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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调用

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2016-07-06 11:42:26

过了一段时间,我把CorsFilter放在第一位,而不是Struts2 Filter,解决了这个问题。现在起作用了。谢谢。

票数 2
EN

Stack Overflow用户

发布于 2018-07-03 09:59:22

U可以在压杆中检查CORS的解。这是一个简单的演示应用程序,解决了struts2中的CORS问题。

https://github.com/rohitmehlawat/struts-cors

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38221689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档