Ajax to the background in JSON-formatted data 415 error causes analysis and solution _ajax related

Source: Internet
Author: User
Tags json uuid

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.