Problem Description:
Ajax returns the JSON format datagram 415 error, as shown in the following figure
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})
@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;
}
Error Analysis: 415 (Unsupported media type) The requested format is not supported by the requested page
The correct JSON format {key:value, Key:value}key, and value should all have double quotes. The data value in the foreground code above has no double quotes, which results in an error (since the previously contacted item is OK to write)
So the preliminary analysis may be the framework of the problem, and some frameworks can be
Data: {"UUID": UUID, "Idcard": Idcard} This is encapsulated in the correct JSON format.
The specific reason is not yet known, the blogger is also small white, and other bloggers know will be shared out
Workaround: Open the code for the foreground annotation
var savedata = json.stringify (Result) <br> This function can be converted into a true JSON format. <br><br>ps: Small white One, have the wrong place please big God to correct, have great God to know
The above is a small set of Ajax to the background to pass the JSON format of the data 415 error causes analysis and solutions, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone!