struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emi <em...@encs.concordia.ca>
Subject ajax for https in struts
Date Fri, 17 Mar 2017 15:57:34 GMT
Hello,

Ajax methods work in http, but NOT https.

Seems due to cross-origin HTTP request.

May I know how to setup "Access-Control-Allow-Origin" in 
struts/jsp/servlet please?


Have tried the following but did NOT work.

(1) mainlayout.jsp
      response.addHeader("Access-Control-Allow-Origin", "*");


(2) web.xml
     <filter>
       <filter-name>cors</filter-name>
       <filter-class>CORSFilter</filter-class>
     </filter>

     <filter-mapping>
        <filter-name>cors</filter-name>
        <url-pattern>/*</url-pattern>
     </filter-mapping>

public class CORSFilter extends OncePerRequestFilter
{
    @Override
    protected void doFilterInternal(
        HttpServletRequest  request,
        HttpServletResponse response,
        FilterChain         filterChain
    )   throws ServletException, IOException
    {
        response.addHeader("Access-Control-Allow-Origin", "*");
        filterChain.doFilter(request, response);
    }
}




example ajax method:

function test(str, url)
{
    var xmlhttp;
    if (str.length==0)
    {
       document.getElementById("txt_hint").innerHTML="";
       return;
    }
    xmlhttp=new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()
    {
       if (xmlhttp.readyState==4 && xmlhttp.status==200)
       {
          var tmp = xmlhttp.responseText;
          document.getElementById("txt_hint").innerHTML = tmp;
       }
    }
    xmlhttp.open("POST", url + "?str=" + str, true);
    xmlhttp.send();
}

Thanks a lot!


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message