java - ajax成功到后台不知道为什么一直回调失败函数
漂亮男人
漂亮男人 2017-05-17 10:06:19
[Java讨论组]
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)
@ResponseBody
public  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;
}
漂亮男人
漂亮男人

全部回复(7)
曾经蜡笔没有小新

请求成功有数据返回,很大可能与你的返回数据格式不对有关系。因为你设置了dataType : 'json' 预期服务器返回的数据类型。这样往往会进入 error 回调。你排除一下返回数据。

而且,error是有三个回调参数的,请自行打印出来。

ajax 跳入error的一些原因

曾经蜡笔没有小新

弹出你的返回值,看看数据就知道了

巴扎黑

HttpServletResponse和ajax的回调冲突了,去掉HttpServletResponse就行。

迷茫

看到你的 dataType : 'json', 要求的是服务器返回json格式,
倘若服务器返回的数据不是json格式的数据,则会走进失败的回调中。

淡淡烟草味

将你AJAX配置dataType:"text",然后用alert(data)查看返回值

由于Ajax请求和response不一样,得到数据后页面不需要再渲染,所以不需要RESPONSE跳转到新页面。所以不需要RETURN,而是通过PrintWriter打印到请求的页面
@RequestMapping(value="/checkIntegral",method = RequestMethod.GET)
@ResponseBody
public 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();

}

ringa_lee

没注意这个ajax是跨域请求的 。

怪我咯

你的返回值数据类型是json,你后台却给他返回了一个Map,把你的map转成json

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号