Springboot--Web application development-cors support

Source: Internet
Author: User

First, Web development often encounter cross-domain problems, solutions are: jsonp,iframe,cors, etc.
CORS compared to JSONP
1. JSONP can only implement GET requests, and CORS supports all types of HTTP requests.
2, using CORS, developers can use ordinary XMLHttpRequest to initiate requests and obtain data, compared to JSONP have a better
Error handling.
3, JSONP is mainly supported by the old browser, they often do not support cors, and most modern browsers have supported Cors

Browser support Scenarios
Chrome
Firefox 3.5+
Opera 12+
Safari 4+
Internet Explorer 8+

Second, you can configure global rules in spring MVC, or you can use @crossorigin annotations for fine-grained configuration

Global configuration:

@Configuration public class Customcorsconfiguration {    @Bean public  webmvcconfigurer corsconfigurer () {   return new Webmvcconfigureradapter () {    @Override public    void Addcorsmappings (Corsregistry registry) {     Registry.addmapping ("/api/**"). Allowedorigins ("http://localhost:8080");    }   ;  

or a

@Configuration public class CustomCorsConfiguration2 extends Webmvcconfigureradapter {  @Override public  Void Addcorsmappings (Corsregistry registry) {   registry.addmapping ("/api/**"). Allowedorigins ("http://localhost:8080");  

Define the method:

@RestController @RequestMapping ("/api") public class Apicontroller {   @RequestMapping (value = "/get")  public Hashmap<string, object> get (@RequestParam String name) {   hashmap<string, object> map = new hashmap< String, object> ();   Map.put ("title", "Hello World");   Map.put ("name", name);   return map;  

Test JS:

$.ajax ({            URL: "Http://localhost:8081/api/get",               Type: "POST",               data: {                   name: "Test"               },               Success:function (data, status, XHR) {                console.log (data);                alert (data.name);               }              });

Fine-grained configuration:

@RestController @requestmapping (value = "/api", method = requestmethod.post) public class Apicontroller {@CrossOrigin ( Origins = "http://localhost:8080") @RequestMapping (value = "/get") public hashmap<string, object> get (@ Requestparam String name) {hashmap<string, object> map = new hashmap<string, object> (); Map.put ("title", " Hello World "); Map.put (" name ", name); return map;}}

  

Springboot--Web application development-cors support

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.