jquery中post回调函数不执行的解决方法

这篇文章将为大家详细讲解有关jquery中post回调函数不执行的解决方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

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\"}

关于“jquery中post回调函数不执行的解决方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。