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

coldplay.xixi
coldplay.xixi原创
2022-12-30 11:12:152206浏览

回调的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中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。