java - Struts2 Ajax CORS -
i'm trying call struts2 action call domain. struts2 on localhost:8080 while 1 make ajax call on one, localhost:3000. i've tried put sample.json
file on struts2 project folder under web folder, can ajax call on sample.json
file. problem is, when i'm trying request action class, error says:
xmlhttprequest cannot load http://localhost:8080/workforce/loginaction. response preflight request doesn't pass access control check: no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:3000' therefore not allowed access.
my problem is, can ajax call on sample json file under same project http://localhost:8080/sample.json
but not struts2 action class?
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); });
updated
i've tried comment out struts2 config in web.xml , create sample servlet.. , works! want know how can configure corsfilter
struts2 filter
after while, i've solved putting first corsfilter
rather struts2 filter
. works. thanks.
Comments
Post a Comment