首页  >  文章  >  web前端  >  jQuery Ajax Post 回调函数不执行怎么办

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

coldplay.xixi
coldplay.xixi原创
2021-01-20 10:49:172793浏览

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

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

本教程操作环境: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("{&#39;errorMsg&#39;:&#39;删除失败&#39;}");
            else writer.write("{&#39;success&#39;:&#39;删除成功&#39;}");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

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

我的:{&#39;hello&#39;:&#39;world&#39;}
标准:{"hello":"world"}

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

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

大功告成!

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

以上是jQuery Ajax Post 回调函数不执行怎么办的详细内容。更多信息请关注PHP中文网其他相关文章!

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