jquery post回调函数不执行怎么解决?

jquery post回调函数不执行怎么解决?下面本篇文章给大家介绍一下jQuery Ajax Post 回调函数不执行问题的解决方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

在使用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\"}

大功告成!

更多web前端知识,请查阅 HTML中文网 !!

以上就是jquery post回调函数不执行怎么解决?的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » jQuery 答疑