jquery中post回调函数不执行的解决办法:JSON数据都要用双引号,由于String不能双引号嵌套使用所以用转义符即可,代码为【{\"hello\":\"world\"}】。

本教程操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。
推荐:jquery视频教程
jquery中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\"}相关免费学习推荐:javascript(视频)
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号