[JS] cross-domain AJAX - the caller and the caller solutions (b) [JS] -JSONP AJAX cross-domain solutions (a) [JS] -JSONP AJAX cross-domain solutions (a)

To solve cross-domain problems

  Cross-domain problem description, reference [JS] -JSONP AJAX cross-domain solutions (a)

  Examples of using the last chapter ( [] JS AJAX cross-domain -JSONP solution (a) ) Examples

  Third solution (callee support cross-domain)

  Callee resolved, based Solutions support cross-domain, based on the relevant provisions Http agreement on cross-domain, in response to the increase in advance of the specified field tells the browser that allows calls

  Cross-domain request is sent directly from the browser to the called party, the called party information in response to an increase in advance to return to the page, the page can request for obtaining content properly.

  1, the server adds a filter (CrossFilter.java), filter all requests, increase in the content request response, as follows:

 1 package com.test.ajax.cross.filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 
14 import org.springframework.util.StringUtils;
15 
16 /**
17  * 服务端解决跨域
18  * @author h__d
19  *
20  */
21 public class CrossFilter implements Filter {
22 
23     @Override
24     public void init(FilterConfig filterConfig) throws ServletException {
25         // TODO Auto-generated method stub
26 
27     }
28 
29     @Override
30     public voidthe doFilter (the ServletRequest Request, the ServletResponse Response, the FilterChain catena alberghiera)
 31 is              throws IOException, ServletException {
 32          the HttpServletResponse RES = (the HttpServletResponse) Response;
 33 is  
34 is          the HttpServletRequest REQ = (the HttpServletRequest) Request;
 35  
36          // support all domain 
37 [          String Origin = req.getHeader ( "Origin" );
 38          IF (! StringUtils.isEmpty (Origin)) {
 39              // support any domain name with a cookie and supports cross-domain calls (caller is cookie domain name, rather than the cookie caller) 
40              RES .addHeader ( "Access-Control-the Allow-Origin" , Origin);
41          }
 42          // specifies a permitted domain belt cookie, origin must be full match can not be used *
 43 is  //         res.addHeader ( "Access-Control-the Allow-Origin", " HTTP: // localhost : 8081") ;
 44          // allow all fields, but can not meet the cross-domain cookie with the request
 45  //         res.addHeader ( "Access-Control-the allow-Origin", "*");
 46 is  
47          // support all custom header 
48          String = req.getHeader headers ( "Access-Control-the allow-headers" );
 49          IF (! StringUtils.isEmpty (headers)) {
 50              // allow all header 
51 is              res.addHeader ( "Access-Control-Allow-Headers", Headers);
 52 is          }
 53 is          // allow all header
 54 is  //         res.addHeader ( "Access-Control-the Allow-Headers", "*");
 55  
56 is          // specified allowedMethod
 57 is  //         res.addHeader ( " --Control-the allow Access methods "," the GET ");
 58          // allow all methods 
59          res.addHeader (" Access-Control-the allow-methods "," * " );
 60          // allows the browser within an hour , the cross-domain cache access information (ie, the top three information) 
61          res.addHeader ( "access-Control-Age-Max", "3600" );
 62          // enable the cookie 
63         res.addHeader("Access-Control-Allow-Credentials", "true");
64 
65         chain.doFilter(request, response);
66 
67     }
68 
69     @Override
70     public void destroy() {
71         // TODO Auto-generated method stub
72 
73     }
74 
75 }

  2, registered filter in web.xml

<filter>
    <filter-name>CrossFilter</filter-name>
    <filter-class>com.test.ajax.cross.filter.CrossFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CrossFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

  3, edit test interface, test3.html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 <script src="jquery-1.11.3.min.js" type="text/javascript"></script>
 7 </head>
 8 <body>
 9     <h2></End testing services to solve cross-domain problemsh2>
10     <a href="#" onclick="get()">发送get请求</a>
11 </body>
12 <script type="text/javascript">
13     function get(){
14         $.getJSON("http://localhost:8080/test-ajax-cross/test/get").then(function(result){
15             console.log(result);
16             $("body").append("<br>" + JSON.stringify(result));
17         });
18     }
19 </script>
20 </html>

  4, enter the address in the browser access, http: //a.com: 8080 / test-ajax-cross / static / test3.html

    

  

  Solution four (callee support cross-domain)

Guess you like

Origin www.cnblogs.com/h--d/p/11478967.html