SPRINGMVC Ajax cross-domain request processing

Source: Internet
Author: User

Last time to write a website to the site before and after the separation of the last kneeling in the Ajax cross-domain on the internet I found a way to try to use the record

Write a class inheritance handlerinterceptoradapter

 PackageCom.util;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.handler.HandlerInterceptorAdapter; Public classCommoninterceptorextendsHandlerinterceptoradapter { Public BooleanPrehandle (httpservletrequest request,httpservletresponse response, Object handler)throwsException {response.setheader ("Access-control-allow-origin", "*"); Response.setheader ("Access-control-allow-methods", "*"); Response.setheader ("Access-control-max-age", "3600"); Response.setheader ("Access-control-allow-headers","Origin, X-requested-with, Content-type, Accept"); Response.setheader ("Access-control-allow-credentials", "true");//whether the browser is allowed to carry user identity information (cookies)return true;}}

Then configure the path in the XML

<mvc:interceptors><!--filtering all requests, handling cross-domain request issues--><mvc:interceptor><mvc:mapping path= "/**"/>< Bean class= "Com.util.CommonInterceptor"/></mvc:interceptor></mvc:interceptors>

That's fine, but there's a blog post that says it's okay to do a simple cross-domain. However, the request for Post+json failed, prompting cross-domain failure. So let's take a look at the way he solves it.

Establish a class inheritance Onceperrequestfilter

 Public classCrossfilterextendsOnceperrequestfilter {@Overrideprotected voidDofilterinternal (HttpServletRequest request, httpservletresponse response, Filterchain Filterchain)throwsservletexception, IOException {if(Request.getheader ("Access-control-request-method")! =NULL&& "OPTIONS". Equals (Request.getmethod ())) {            //CORS "pre-flight" RequestResponse.AddHeader ("Access-control-allow-origin", "*"); Response.AddHeader ("Access-control-allow-methods", "GET, POST, PUT, DELETE"); Response.AddHeader ("Access-control-allow-headers", "Content-type"); Response.AddHeader ("Access-control-max-age", "1800");//min} filterchain.dofilter (request, response); }}

Web. XML inside Set

<filter>    <filter-name>cors</filter-name>    <filter-class>cn.***.filter. crossfilter</filter-class></filter><filter-mapping>    <filter-name>cors< /filter-name>    <url-pattern>/*</url-pattern></filter-mapping>

Then you can do the above way for the spring3.0 version if the 4.0 version can be used in the following way (no test)

XML inside Configuration

    <mvc:cors>        <mvc:mapping path= "/**" allowed-origins= "*" allow-credentials= "true" max-age= "1800" allowed-methods= "Get,post,options"/>    </mvc:cors>

Part of the blog http://www.cnblogs.com/asfeixue/p/4363372.html of Flying Ang Snow

SPRINGMVC Ajax cross-domain request processing

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.