我送使用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'的?初始化它的代碼在哪裏? –