When sending a JSON package from a separate HTML file to Java Spring RestController using jQuery, java issues a warning:
Content type 'application / x-www-form-urlencoded; charset = UTF-8' not supported]
In this case, all MediaType listed.
Chrom gives 2 more errors:
POST http: // localhost / post 415
and
Access to XMLHttpRequest at ' http: // localhost / post ' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header
Controller Code:
@RestController public class mainController { @RequestMapping(value = "/post", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) public ResponseEntity<Object> postUser(@RequestBody User user){ System.out.println(user); return new ResponseEntity<>("Success", HttpStatus.OK); } } JQery code:
$(document).ready(function() { $('#go').on('click', function() { var user = { "name" : "Tom", "age" : 23 }; $.ajax({ url: 'http://localhost/post', type: 'POST', dataType: 'json', data: user }) .done(function() { console.log("success"); }) .fail(function() { console.log("error"); }) .always(function() { console.log("complete"); }); }); }); A huge request to tell where is the error and how to solve this problem?
PS T.K. I don’t have enough experience to say how such applications are written, namely Backend first and then using Thymeleaf or Mustache Front templates, or is Frontend completely written separately like mine?