0
我想通過ajax搜索一個帳戶。我在客戶端有這個。jQuery的Spring MVC AJAX無法檢索json響應
<form id="fetchAccount">
<input type="search" name="id"/>
<input type="submit"/>
</form>
<script>
$(document).ready(function(){
$('#fetchAccount').on('submit', function(e){
var data = $(this).serialize();
var header = $("meta[name='_csrf_header']").attr("content");
var token = $("meta[name='_csrf']").attr("content");
$.ajax({
data:data,
type:"PUT",
url:"fetchAccout",
dataType : 'json',
contentType : 'application/json; charset=utf-8',
beforeSend: function(xhr){
xhr.setRequestHeader(header, token);
},
success: function(response){
console.log(response);
}
});
e.preventDefault();
});
});
</script>
和具有200的HTTP響應控制器
@RequestMapping(value="/fetchAccout", method=RequestMethod.PUT,
produces= MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Account fetchAccount(@RequestParam(value="id", required=false) Long id){
Account account = null;
if(id != null)
account = custService.findAccountById(id);
return account;
}
林上。我懷疑我的錯誤在客戶端。我甚至不能在成功功能中做出警報。
哦對不起錯字。我真的這樣做,但仍然不能看到響應對象.. –
你檢查findAccountById實際上是否返回任何東西嗎?你的ID實際上是否返回null?我會取出if(id!= null)行並測試它。 – Aeseir
哦,我發現ID在im檢索requestparam是空的。 –