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:
The unit concatenated aftervar requestUrl = "http://localhost:8085/femis/unitInsertSimple/"+ unit; this.http .get(requestUrl) .subscribe(data=>this.searchData());//searchData()
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:
The model concatenated later invar requestUrl = "http://localhost:8085/femis/unitUpdateByPrimaryKey/" + model; this.http.get(requestUrl).subscribe(data=>this.searchData());
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:
- resolve cross-source errors in the above modification request
- 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
- 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.