Cause Analysis and Solution for 415 error in json format data uploaded by Ajax to the background, json415
Problem description:
Error 415 when ajax uploads a json-format datagram to the background, as shown in
Page code
function saveUser(){var uuId = document.getElementById("uuid").value;var idCard = document.getElementById("idCard").value;alert(uuId+idCard);// var result = new Object();// result.uuId = uuId;// result.idCard = idCard;// var saveData = JSON.stringify(result);// alert(saveData);$.ajax({url : "xdds/saveUser.do?random=" + Math.random(),type : "post",data : {"uuid" : uuId,"idCard" : idCard},// data:saveData,dataType : 'json',// contentType : "application/json",success:function(data){}});}
Background code
@RequestMapping(value = "/saveUser.do", method = { RequestMethod.POST })@ResponseBodypublic Map<String, Object> saveUser (@RequestBody MapUser user){Map<String, Object> map = new HashMap<String, Object>();System.out.println(user.getUuid()+user.getIdCard());map.put("result", "fda");return map ;}
Error Analysis: 415 (unsupported media type) request format is not supported by the request page
The correct json format {key: value, key: value} key and value should both contain double quotation marks. The data value in the preceding front-end code data does not contain double quotation marks, so an error is reported (because the previously accessed project is correct)
Therefore, preliminary analysis may be about the framework. Some frameworks can
Data: {"uuid": uuId, "idCard": idCard} is encapsulated in the correct json format.
I still don't know the specific reason. The blogger is also Tom. I will share it with you when the blogger understands it.
Solution: Open the Code Annotated at the front end
Var saveData = JSON. stringify (result) <br> this function can be converted to a real json format. <Br> ps: please correct me if something is wrong.
The above section describes the Cause Analysis and Solution for the 415 error in uploading json data to the backend Using Ajax. I hope this will help you, if you have any questions, please leave a message. The editor will reply to you in time!