Springboot cross-domain request ajax jsonp

SpringBoot configuration:

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

@SuppressWarnings ( "deprecation" ) 
@Configuration 
public  class CorsFilter the extends WebMvcConfigurerAdapter { 

    @Override 
    public  void addCorsMappings (CorsRegistry Registry) { 
        registry.addMapping ( "/ **" ) .allowedHeaders ()
                 // allows use request method, a comma separated open 
                .allowedMethods ( "*" )
                 // acceptance of any domain request 
                .allowedOrigins ( "*" )
                 // indicate whether to allow sending Cookie. Cookie default CORS not included in the request. Presentation server express permission, Cookie can contain sent to the server along with the request when set to true. 
                .allowCredentials ( to true)
                 // number of seconds to cache the request. Within this time, all requests of the same type will not send the request but the preflight return the head to be used directly as the basis for judgment, very useful, a substantial number of optimization request 
                .maxAge (3600 ); 
    } 
}
@SpringBootApplication
@ComponentScan
public class CorsDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(CorsDemoApplication.class, args);
    }

}
@Controller
@RequestMapping("/cors")
public class CorsController {

    @RequestMapping(value="/index")
    @ResponseBody
    public String corsTest(HttpServletRequest request,String name,String age) {
        String jsonpCallback = request.getParameter("callback");
        Map<String, Object> map=new HashMap<>();
        map.put("name",name);
        map.put("age",age);
        map.put("date",new Date());
        return jsonpCallback + "(" + JSON.toJSONString(map) + ")";
    } 
}

Ajax request:

          .ajax $ ({ 
                            URL: "http://62.234.65.61:8090/cors/index" , 
                            type: "the POST" , 
                            Data: {
                                  "name": "admin section" ,
                                  "Age": 20 is 
                            }, 
                            dataType: " jsonp " ,
                             / * jsonp:" the callback ", // passed to the request handler, or pages, to obtain the name of the callback function jsonp parameter name (default is typically: the callback) 
                            jsonpCallback:" the callback ", // custom jsonp callback function name, the default name for the random function jQuery automatically generated, you can also write "?", JQuery will automatically process the data for you * / 
                            Success: function (Data) {
                                 console.log("res:"+JSON.stringify(data));
                            },
                            error: function(){
                                 alert('fail');
                            }
                        });

to sum up:

Test 1: background + foreground running locally, IP port different from the same test results: res: { "date": 1560913652938, "name": "amdin", "age": "20"}

Test 2: Local preceding operation, the background into the cloud server Tencent, the test results: res: { "date": 1560913652938, "name": "amdin", "age": "20"}

Cross-domain requests can be completed successfully and get the return value

Guess you like

Origin www.cnblogs.com/JQKA/p/11050245.html