ajaxTransmitting json format data to the background reports a 415 error. What causes it and how to solve it? Below, the editor of Script House brings you Ajax Analysis and solutions to the causes of 415 errors when transmitting data in json format to the background. Friends who are interested in ajax, let’s take a look.
Problem description:
Ajax transmits json format data to the background and reports a 415 error, as shown in the figure below
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){ } }); }
@RequestMapping(value = "/saveUser.do", method = { RequestMethod.POST }) @ResponseBody public 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 ; }
framework , some frameworks can encapsulate
data : {"uuid" : uuId,"idCard" : idCard} into the correct json format. The specific reason is not yet known. The blogger is also a novice. I will share it when the blogger figures it out.Solution: Add Comment## to the front desk #'s code opens
var saveData = JSON.stringify(result)<br>这个函数可以转化成真确的json格式。<br><br>ps:小白一个,有不对的地方请大神指正;有大神知道具
The above is the Ajax that the editor introduces to you to pass json to the background Analysis and solutions to the causes of 415 errors in formatted data. I hope it will be helpful to everyone! !
How to get the json array length in jsHow can ajax read local json How Ajax transmits Json and xml data
The above is the detailed content of Analysis and solutions to the causes of 415 errors when Ajax transmits json format data to the background. For more information, please follow other related articles on the PHP Chinese website!