这是我的带有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 Controller 方法
@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) 正确打印该值。此外, Controller 被成功调用, Controller 方法中的所有代码都按照其必须执行的方式执行,并且 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上,这是我所相信的。 提前致谢!
最佳答案
问题很可能出在 Spring MVC 渲染 View 时(检查日志)。由于您的方法返回 String,Spring 希望它是要呈现的 View 的名称。 如果你想返回Json,我建议你返回POJO List并使用@ResponseBody作为注释中建议的sp00m注释方法
关于jquery - 使用 Spring MVC 进行 Ajax 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25122251/