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. mysql - 我的myeclipse一直连显示数据库连接失败,不知道为什么2. docker绑定了nginx端口 外部访问不到3. javascript - 关于Js中 this的一道题4. javascript - H5页面怎么查看console信息?5. ubuntu 远程管理KVM设置问题6. javascript - vue生成一维码?求助!!!!!急7. javascript - 修改表单多选项时和后台同事配合的问题。8. angular.js - angularjs 怎么封装 upload 上传9. 网页爬虫 - Python:爬虫的中文编码问题?10. dockerfile - 我用docker build的时候出现下边问题 麻烦帮我看一下
