我送使用AJAX和我的Spring MVC的控制器越来越null
价值形式的数据,但如果我用Postman它将很好地工作。不知道为什么AJAX表单数据发送空到MVC控制器
这里是我的Ajax调用:
$.ajax({
type: "post",
url:"http://localhost:8080/fivenet/mobile/api/login",
crossDomain: true,
data: JSON.stringify(formData),
dataType: 'json',
contentType: 'text/html;charset=UTF-8',
//contentType: 'application/json;charset=utf-8',
beforeSend: function(xhr){
},
success: function(msg){
console.log('sucess');
},
error: function(msg){}
这里是我的控制器:
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "create-account", method = RequestMethod.POST)
public @ResponseBody RespondMessage createAccount(
@RequestParam(value = "data", required = true) String dataString,
HttpServletRequest request) throws Exception {
RespondMessage responsed = new RespondMessage();
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
JSONObject data = new JSONObject(dataString);
return responsed;
}
是否错误在JavaScript控制台中发生的呢?你能准确地发布你在POSTman中使用过的参数吗? – matthewninja
是的,它返回500,当我在调试模式下运行我的弹簧时,“String dataString”返回null。参数看起来像这样{username}:“[email protected]”, “password”:“12345” } –
你是如何填充'formData'的?初始化它的代码在哪里? –