I am trying to get the values of a field from a jsp to a spring mvc controller. The values from the jsp are not in a form, so I can't use HttpServletRequest to get the values directly. So I get the values in js using document.getElementById('fieldName').value, create an object, strigify it and send it to the controller using AJAX. However, I am getting a 415 (Unsupported Media Type) error. Below is my js code and controller code:
function getInput(){
var ageMin=document.getElementById('age_min').value;
var ageMax=document.getElementById('age_max').value;
var creditMin=document.getElementById('credit_min').value;
var obj = {"ageMin":ageMin,"ageMax":ageMax, "creditMin":creditMin};
console.log(obj);
var sobj=JSON.stringify(obj);
console.log(sobj);
$.ajax({
url: "http://localhost:8080/login/validate.mvc",
type: 'POST',
data: sobj,
contentType: "application/json; charset=utf-8",
success: function(results) {
}
});
}
Controller:
@RequestMapping(value = "/validate.mvc", method = RequestMethod.POST )
public void getValidate(@RequestBody Object sobj){
System.out.println(sobj);
}
只需指定数据类型属性即可解决您的问题
datatype : "json",
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.