• 技术文章 >web前端 >js教程

    jQuery Ajax Post 回调函数不执行怎么办

    coldplay.xixicoldplay.xixi2021-01-20 10:49:17原创502

    回调的JSON数据格式问题,导致回调函数一直无法执行;jQuery Ajax Post 回调函数不执行的解决办法:JSON数据都要用双引号,使用转义字符转义String,代码为【{\"hello\":\"world\"}】。

    本教程操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑,该方法适用于所有品牌电脑。

    推荐:jquery视频教程

    jQuery Ajax Post 回调函数不执行的解决办法:

    1、前台代码

    $.post('${pageContext.request.contextPath}/user_deleteUser',{uid:row.uid},function(result){
                                if (result.errorMsg){
                                    $.messager.show({    
                                        title: 'Error',
                                        msg: result.errorMsg
                                    });
                                } else {
                                    $('#dg').datagrid('reload');    
                                }
                            },'json');

    2、后台代码

    public String deleteUser() {
            int count = userDao.deleteUser(model.getUid());
            try {
                PrintWriter writer = response.getWriter();
                if(count<=0) writer.write("{'errorMsg':'删除失败'}");
                else writer.write("{'success':'删除成功'}");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

    很明显前台代码并没有什么问题,后台代码在逻辑上貌似也没什么问题,最后百度得知回调的JSON数据格式问题,导致回调函数一直无法执行,原来JSON数据都要用双引号!

    我的:{'hello':'world'}
    标准:{"hello":"world"}

    由于String不能双引号嵌套使用所以我们用转义符即可

    {\"hello\":\"world\"}

    大功告成!

    相关免费学习推荐:js视频教程

    以上就是jQuery Ajax Post 回调函数不执行怎么办的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:jQuery 回调函数
    上一篇:react中get与post的区别是什么 下一篇:jquery如何使用css方法让按钮不可点击
    大前端线上培训班

    相关文章推荐

    • js回调函数是什么• 详解JavaScript中的回调函数• jquery中post回调函数不执行怎么办• 详细了解JavaScript中的回调函数

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网