2016-07-06 243 views
0

我想從另一個域調用我的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。而它的作品!我只是想知道我怎麼可以配置CorsFilterStruts2 Filter

+0

http:// localhost:8080/workforce/sample.json此網址的作品,我可以檢索其中的數據。 –

+0

其實,我已經將它添加到了我的web.xml中,我只是沒有放在那裏,但我已經更新了我的答案以顯示我的S2配置。我是否需要在動作課上實施CorsFilter?我只是把它放在我的web.xml中,沒有任何內容。 –

+0

你需要調試才能看到 - >你的CorsFilter被調用動作url嗎?什麼關於json文件的網址? –

回答

1

一段時間後,我已經把第一的CorsFilter而不是Struts2 Filter解決它。現在起作用了。謝謝。

+0

這就是爲什麼有評論:*你確定你有完全相同的web.xml,你已經顯示?* –

+0

我的壞,我剛剛添加struts2配置時,你已經請求它。但是,最初它在CorsFilter之上。謝謝 –