这是我的带有Ajax请求的HTML页面。
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<script type="text/javascript">
var a_id;
if (location.search) {
a_id = location.search.substring(1);
document.write(a_id);
}
$.ajax({
type:'GET',
url: "http://192.168.1.5:8080/SurveyApp3/getSingleAgent?a_id="+a_id,
dataType: 'json',
success:function(data, status){
console.log(data);
},
error:function(xhr, status, errorThrown){
console.log(xhr);
console.log(status);
console.log(errorThrown);
}
});
</script>
以下是我的Spring MVC控制器方法
@RequestMapping(value= "/getSingleAgent", method= RequestMethod.GET)
public String getSingleAgents(@RequestParam("a_id") long a_id){
List<Agents> agent= SpringController.getInstance().getSingleAgent(a_id);
JSONObject json= new JSONObject(agent.get(0));
System.out.println(json);
return json.toString();
}
调用ajax请求之前,document.write(a_id)已正确打印出值。同时,控制器也被成功调用,方法内的所有代码都按照所需方式执行,System.out.println(json);也打印出了所需数据。
我的问题出在ajax请求上。ajax没有调用success:function,而是调用了error:function,并在日志中出现了以下错误。
08-04 20:39:05.191: D/CordovaLog(32740): [object Object]
08-04 20:39:05.191: I/chromium(32740): [INFO:CONSOLE(36)] "[object Object]", source: file:///android_asset/www/updateAgentForm.html?9999999999 (36)
08-04 20:39:05.191: D/CordovaLog(32740): error
08-04 20:39:05.191: I/chromium(32740): [INFO:CONSOLE(37)] "error", source: file:///android_asset/www/updateAgentForm.html?9999999999 (37)
08-04 20:39:05.201: D/CordovaLog(32740): Not Found
08-04 20:39:05.201: I/chromium(32740): [INFO:CONSOLE(38)] "Not Found", source: file:///android_asset/www/updateAgentForm.html?9999999999 (38)
我哪里做错了?我认为问题在于ajax。
提前感谢!
getSingleAgents
方法上添加@ResponseBody
。 - sp00m