Home > Web Front-end > JS Tutorial > body text

Analysis and solutions to the causes of 415 errors when Ajax transmits json format data to the background

韦小宝
Release: 2017-12-30 18:41:14
Original
4175 people have browsed it

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){
}
});
}
Copy after login


## Backend 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 ;
}
Copy after login


Error analysis: 415 (Unsupported media type) The requested format is not supported by the requested page

Correct json format {key:value, key:value} Both key and value should be enclosed in double quotes. The data value in the front-end code data above does not have double quotes, so an error is reported (because it is OK to write the project in this way)

So the preliminary analysis may be a problem with the

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:小白一个,有不对的地方请大神指正;有大神知道具
Copy after login


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! !

Related recommendations:

How to get the json array length in js

How 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!