Half of the cross-source / cross-domain problems have been solved.

the cross-source / cross-domain problem is half solved?

  • my development environment:
    uses VS Code to develop front-end code, front-end uses Angular6
    Java project with SpringBoot + Mybatis back-end
    both front and back ends are developed by myself on a computer
  • HttpClient is used in the Angular code of the front end to send a request to get data. The new request is:

    var requestUrl = "http://localhost:8085/femis/unitInsertSimple/"+ unit;
    this.http
        .get(requestUrl)
        .subscribe(data=>this.searchData());//searchData()
    The unit concatenated after

    is of type string. The code in the Controller that accepts the request on the Java side is:

        @GetMapping("/unitInsertSimple/{unit}")
        public String insert(@PathVariable("unit") String unit) {
            return service.insert(unit);
        }

    the header of the Controller class is annotated: @ CrossOrigin, and @ RestController
    passed the test of the new request above, and successfully added a sum of data to DB

  • Let"s talk about the problematic request-modification request:
    the modification request in Angular is:

    var requestUrl = "http://localhost:8085/femis/unitUpdateByPrimaryKey/" + model;
    this.http.get(requestUrl).subscribe(data=>this.searchData());
    The model concatenated later in

    url is a model object, and its code is:

    export class Unit {
      constructor(
        public sId:string    
      ){}
    }

    the Java side processes the request as follows:

        @GetMapping("/unitUpdateByPrimaryKey/{unit}")
        public String updateByPrimaryKey(@PathVariable("unit") Unit unit) {
            return service.updateByPrimaryKey(unit);
        }

    the Unit in the Java code above is the model class. The error of the request for modification is reported in the browser as follows:

    : http://localhost:8085/femis/unitUpdateByPrimaryKey/[object%20Object] :CORS  "Access-Control-Allow-Origin"
    
  • my doubt:
    the new request and modification request mentioned above are in a ts file, and the new request can be executed normally. Why do I report a cross-source error when it comes to the modification request? Does
    have anything to do with the parameters concatenated after the request? (string type is concatenated after the new request, and objects are joined after the modification request)
  • desired help:

    1. resolve cross-source errors in the above modification request
    2. Why don"t you just follow the object variable directly after the url in the get request, just like the code above? What should the correct format look like
    3. if the get request is not suitable for transferring objects, then the post method should be used? If so, how should I write the post method to pass the object?
      forgive me for being a rookie developed by WEB.
  • if necessary:
    my contact information: the number in front of 409223171@qq.com is my QQ number. If you add my words, please indicate: cross-source, for fear that you will ignore it, you must add it.
Jun.07,2021

angular2 has not been used since. Please refer to the following instructions:

1: resolve the cross-source error in the above modification request: I used springboot 2.* to add a cross-domain configuration. If I use HTTP.OPTION request to return 200 , the configuration is successful

.
@Configuration
public class AppConfiguration {
    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(false);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }
}

2 and 3. If it's too complicated, use post or follow rest . If you have to send an object with get , you need to convert it to a string with JSON , but it's really not elegant enough.


is actually a problem. You don't have to request an object to be appended to the path, just use the form to submit it. This post ends here. To see how angular6+antd+java+springmvc submits form data and automatically encapsulates it into bean, please see
https://codeshelper.com/q/10.

.
Menu