java - ajax成功到后台不知道为什么一直回调失败函数
问题描述
function a() {$.ajax({url : 'http://localhost:8080/ubi/checkIntegral',async : true,data:{'carOwnerID':'111111'},dataType : ’json’,type : ’GET’,success : function() { alert('ss');},error : function(map){ alert('FALSE');} });}@RequestMapping(value='/checkIntegral',method = RequestMethod.GET)@ResponseBodypublic Map<String,Long> checkIntegral(@RequestParam String carOwnerID ,HttpServletRequest request,HttpServletResponse response){ Long integral = impl.checkIntegral(Long.valueOf(carOwnerID)); Map<String,Long> map = new HashMap<String, Long>(); map.put('msg', integral); return map;}
问题解答
回答1:请求成功有数据返回,很大可能与你的返回数据格式不对有关系。因为你设置了dataType : ’json’ 预期服务器返回的数据类型。这样往往会进入 error 回调。你排除一下返回数据。
而且,error是有三个回调参数的,请自行打印出来。
ajax 跳入error的一些原因
回答2:弹出你的返回值,看看数据就知道了
回答3:HttpServletResponse和ajax的回调冲突了,去掉HttpServletResponse就行。
回答4:看到你的 dataType : ’json’, 要求的是服务器返回json格式,倘若服务器返回的数据不是json格式的数据,则会走进失败的回调中。
回答5:将你AJAX配置dataType:'text',然后用alert(data)查看返回值
由于Ajax请求和response不一样,得到数据后页面不需要再渲染,所以不需要RESPONSE跳转到新页面。所以不需要RETURN,而是通过PrintWriter打印到请求的页面@RequestMapping(value='/checkIntegral',method = RequestMethod.GET)@ResponseBodypublic void checkIntegral(@RequestParam String carOwnerID ,HttpServletRequest request,HttpServletResponse response){
Long integral = impl.checkIntegral(Long.valueOf(carOwnerID)); PrintWriter writer=response.getWriter(); writer.write(String.valueOf(integral)); writer.flush(); writer.close();
}
回答6:没注意这个ajax是跨域请求的 。
回答7:你的返回值数据类型是json,你后台却给他返回了一个Map,把你的map转成json
相关文章:
1. python - (初学者)代码运行不起来,求指导,谢谢!2. 为什么python中实例检查推荐使用isinstance而不是type?3. mysql里的大表用mycat做水平拆分,是不是要先手动分好,再配置mycat4. window下mysql中文乱码怎么解决??5. sass - gem install compass 使用淘宝 Ruby 安装失败,出现 4046. html5 - H5 SSE的本质是什么?7. javascript - h5上的手机号默认没有识别8. python - 获取到的数据生成新的mysql表9. python的文件读写问题?10. javascript - js 对中文进行MD5加密和python结果不一样。
