Solving the problem of Ajax cross-domain requests using @crossorigin annotations in SPRINGMVC

Source: Internet
Author: User

1. What is a cross-domain

Cross-domain, or cross-site HTTP requests (Cross-site HTTP request), refers to the domain in which the requested resource originated differs from the request to the HTTP request pointing to the domain of the resource.

2. Cross-domain application scenarios

When using front-end separation and backend-led development in front-end collaborative development, there are often the following scenarios:

    1. Backend development is completed on the server and deployed to the front-end API documentation.
    2. The front-end is developed locally and requests are sent to the backend deployed on the remote server.
      In this development process, if the front end wants to test the interface while developing, it needs to use a cross-domain approach.
3. Annotate to allow cross-domain

Very simple, we can annotate the controller class or its methods @CrossOrigin to enable it to support cross-domain.
Example:

@CrossOrigin(origins = "*", maxAge = 3600)@RestController@RequestMapping("/User")public class UserController {}

Where origins is the default parameter for Crossorigin, that is, the cross-domain source, * that is, any source, or another domain name. That can be in the following form:

@CrossOrigin("http://test.com")@CrossOrigin(origins="http://test.com",maxAge=3600)

This annotation is used on methods, the same way, and when processed, SPRINGMVC the label on the class and on the method.

Solving the problem of Ajax cross-domain requests using @crossorigin annotations in SPRINGMVC

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.