JSP Ajax Cross-domain problem how to deal with the reason: CORS header missing ' Access-control-allow-origin '). Ajax HTTP 415

Source: Internet
Author: User

/**
* Project Name:cm2mmanage
* File Name:CrossSiteFilter.java
* Package Name:com.yoxnet.serverframework.base
* date:2016 April 27 4:52:51
* Copyright (c), [email protected] All rights Reserved.
*
*/

Package com.zhl.sms.filter;

Import java.io.IOException;
Import java.util.Enumeration;

Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;

/**
* Classname:crosssitefilter <br/>
* Function:todo ADD Function. <br/>
* Reason:todo ADD Reason. <br/>
* date:2016 April 27 pm 4:52:51 <br/>
* @author YYC
* @version
* @since JDK 1.6
* @see
*/
public class Crosssitefilter implements filter{

Private Logger logs = Loggerfactory.getlogger (This.getclass ());

@Override
public void init (Filterconfig filterconfig) throws Servletexception {

}

@Override
public void DoFilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
HttpServletRequest req = (httpservletrequest) request;
Enumeration E = Req.getheadernames ();
String headvalue = "";
while (E.hasmoreelements ()) {
Headvalue = (String) e.nextelement ();
String value = Req.getheader (Headvalue);
System.out.println (headvalue+ "=" +value);
// }
Logs.debug (">>>>>>>>>>>>>>> header information for this request is as follows:");
while (E.hasmoreelements ()) {
String name = (string) e.nextelement ();
String value = Req.getheader (name);
Logs.debug (name+ "=" +value);
}
Logs.debug (">>>>>>>>>>>>>>> End of header information for this request");
HttpServletResponse resp = (httpservletresponse) response;
HttpSession session = Req.getsession ();
Resp.addheader ("Access-control-allow-origin", "*");
Resp.addheader ("Access-control-allow-origin", "http://192.168.1.170");
Resp.addheader ("Access-control-allow-methods", "GET, POST, PUT, DELETE, OPTIONS");
Resp.addheader ("Access-control-allow-credentials", "true");
Resp.addheader ("Access-control-allow-headers", "X-requested-with,content-type");
Resp.addheader ("Access-control-max-age", "1800");//30 min
if (Req.getheader ("Access-control-request-method")! = null && "OPTIONS". Equals (Req.getmethod ())) {

}
Chain.dofilter (request, response);
}

@Override
public void Destroy () {

}

}

In the XML configuration:

<filter>
<filter-name>crossSiteFilter</filter-name>
<filter-class>com.zhl.sms.filter.CrossSiteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>crossSiteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

JSP Ajax Cross-domain problem how to deal with the reason: CORS header missing ' Access-control-allow-origin '). Ajax HTTP 415

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.