调试代码遇到一个问题,就是前台运行删除操作后,controller返回数据,但前台接收时,ajax不运行success回调,总是弹出失败的对话框.接收数据类型是json.

先看看我的前台代码.

if (rows) {
$.messager.confirm('警告', '确定删除吗?', function(r) {
if (r) {
$.ajax({
type : 'post',
url : 'deleteStudentTeachClass',
data : {
"ids" : ids
},
dataType : 'json',
traditional : true,
success : function(result) {
$.messager.alert("提示", "恭喜您,删除成功", "info");
$("#dg").datagrid("reload");
},
error : function(msg) {
$.messager.alert("提示", "操作失败", "info");
$("#dg").datagrid("reload");
} });
}
});
}

以下是后台controller代码

@RequestMapping(value = "/deleteStudentTeachClass")
public void deleteStudentTeachClass(String ids, HttpServletRequest request,
HttpServletResponse response) throws Exception { String dataBaseName = "itoo_platform";
String[] strArray = null;
strArray = ids.split(",");
Boolean flag = false;
String result = "false";
try {
flag = schoolTeachingBean.deleteStudentTeachClass(strArray,
dataBaseName);
if (flag == true) {
result = "success";
}
} catch (RuntimeException e) {
e.printStackTrace();
}
outToJson.outJson(response, result);
}

通过查询发现dataType例如以下的说明:

"json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

也就是说jquery1.4版本号以后对json格 式要求很严格,要满足json.org站点定义的格式才干运行success回调,否则都会出错。无法解析返回的json数据.我看了下返回到前台的字符串,的确不是严格的json格式.





于是把后台返回值改成了这样:

if (flag == true) {
result = "{\"result\":true}";
}

但不管返回true还是false都运行success回调,这让我更郁闷.百思不得其解.

终于把前台推断改成了这样:

if (rows) {
$.messager.confirm('警告', '确定删除吗?', function(r) {
if (r) {
$.ajax({
type : 'post',
url : 'deleteStudentTeachClass',
data : {
"ids" : ids
},
dataType : 'text',
traditional : true,
success : function(result) {
if(result=='true'){
$.messager.alert("提示", "恭喜您,删除成功", "info");
$("#dg").datagrid("reload");
}
else{
$.messager.alert("提示", "操作失败", "info");
$("#dg").datagrid("reload");
}
} });
}
});
}

ajax不运行success回调而是运行error回调的更多相关文章

  1. ajax不执行success回调而是执行了error回调

    最近在看jQuery的API文档,在使用到jQuery的ajax时,如果指定了dataType为json,老是不执行success回调,而是执行了error回调函数.   附上代码如下: JScrip ...

  2. jquery ajax 后台响应成功,返回正确json但不执行success方法,执行error的问题

    昨天被这问题卡了好几个小时.查看http状态码:是200.而且返回了预想的json字符串.但执行的是error方法,不执行success方法.在网上查了一下,才发现是后台页面返回的json字符串格式不 ...

  3. Zend Server安装后首次运行就出现Internal Server Error的解决

    无论是使用哪个版本的Zend Server来搭建PHP服务器,首次运行都会出现Internal Server Error的错误,对很多新手而言,每当看到这种错误时,那一刻内心绝对都是崩溃的.然而,这个 ...

  4. 解决IIS7运行ASP提示错误:An error occurred on the server when processing the URL. Please contact the system administrator

    原文:解决IIS7运行ASP提示错误:An error occurred on the server when processing the URL. Please contact the syste ...

  5. Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer

    Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer 解决办法: 1.  Start heka ...

  6. VMware运行gazebo,关于vmw_ioctl_command error Invalid argument错误

    运行rviz 时报  VMware: vmw_ioctl_command error 无效的参数.  这个错误. 或者 运行gazebo,关于vmw_ioctl_command error Inval ...

  7. 新建SpringBoot项目运行页面报错Whitelabel Error Page This application has no explicit mapping for /error, so yo

    新建SpringBoot项目运行页面报错Whitelabel Error Page This application has no explicit mapping for /error, so yo ...

  8. 运行android模拟器,emulator: ERROR: x86 emulation currently requires hardware acceleration!

    运行android模拟器,emulator: ERROR: x86 emulation currently requires hardware acceleration! 问题: 运行android模 ...

  9. 终于解决了Linux下运行OCCI程序一直报Error while trying to retrieve text for error ORA-01804错误

    终于解决了Linux下运行OCCI程序一直报Error while trying to retrieve text for error ORA-01804错误 http://blog.csdn.net ...

随机推荐

  1. 慎用Java递归调用

    总结:慎用Java递归调用,测试时可以尝试该方法,否则尽量不要使用递归!递归过多调用时,最好改为for或者whlie来代替. 在java语言中,使用递归调用时,如果过多的调用容易造成java.lang ...

  2. Nodejs解决所有跨域请求

    Nodejs解决所有跨域请求 app.use(function(req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); ...

  3. Java中导入导出Excel -- POI技术

    一.介绍: 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实 ...

  4. python提纲

    根据网上专栏整理提纲 1.模块介绍 2.time&datetime模块 3.random模块 4.os模块 5.sys模块 6.json&pickle模块 7.logging模块 8. ...

  5. Mathematica作图

    第2讲 在Mathematica中作图    一个较强的符号计算系统均有很好的绘图功能,Mathematica也不例外,Mathematica 拥有非常强大的绘图功能.并且提供了一大批基本数学函数的图 ...

  6. [win7] 带网络的安全模式,启动QQEIMPlatform第三方服务

    REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\QQEIMPlatform" /VE /T REG_ ...

  7. Codeforces 810 A.Straight «A»

    A. Straight «A»   time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  8. 神器mimikatz使用命令方法总结

    神器mimikatz使用命令方法总结 文章地址:http://www.isharepc.com/300.html mimikatz是一款功能强大的轻量级调试神器,通过它你可以提升进程权限注入进程读取进 ...

  9. HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submis ...

  10. [BZOJ2655]calc(拉格朗日插值法+DP)

    2655: calc Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 428  Solved: 246[Submit][Status][Discuss] ...