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是空的。 –