您的位置:首页技术文章
文章详情页

java - ajax成功到后台不知道为什么一直回调失败函数

浏览:54日期:2023-12-03 17:34:06

问题描述

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

标签: java
相关文章: