回调的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 回调函数不执行怎么办的详细内容。