调试代码遇到一个问题,就是前台运行删除操作后,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. js中给easyui的一列添加按钮

    $("#totalTb").datagrid({ columns: [[                { field: 'ENTITY_ACTNAME', title: '活动名 ...

  2. RedHat修改系统时区

    http://blog.itpub.net/27099995/viewspace-1370723/ 今天又被开发的说服务器时间异常,时差很大.我就纳闷了,上个星期都调整过的.去查看了一下. [root ...

  3. 操作MySQL数据库相关代码

    注意事项: 1.导入驱动包,如我导的是mysql-connector-java-5.1.26-bin.jar 2.修改下面代码中的用户名和密码 3.其中URL为"jdbc:mysql://数 ...

  4. 1.kafka的介绍

    kafka是一种高可用,高吞吐量,基于zookeeper协调的分布式发布订阅消息系统. 消息中间件:生产者和消费者 举个例子: 生产者:做馒头,消费者:吃馒头,数据流:馒头 如果消费者宕机了,吃不下去 ...

  5. python基础复习-1-1文件类型、变量、运算符、表达式

    文件类型: .py python源文件 由python解释器执行 .pyc python源码编译后生成的文件(字节代码) 编译方法: 源码文件中使用py_compile模块 import py_com ...

  6. sql ---- count 误区

    select count(字段名) from table group by 字段; 分组后的内容统计. count统计的字段是 不包括空的数据.

  7. webpack + react 前端工程化实践和暂不极致优化

    技术结构 webpack + react + react-router 功能实现 关于打包 1.基于react-router的自定义打包code split.2.分包异步按需加载.3.CommonsC ...

  8. css :not 选择器

    :not 选择器是css3里面的 :not([class]){color:red;}  // 没有class属性的元素都设置为红色 p:not([class]){color:red;} // 没有cl ...

  9. Problem C: #104. 普通平衡树

    #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #inc ...

  10. 【数论】【欧拉函数】bzoj2190 [SDOI2008]仪仗队

    由图可知,一个人无法被看到时,当且仅当有 人与原点 的斜率与他相同,且在他之前. ∴一个人可以被看到,设其斜率为y/x,当且仅当y/x不可再约分,即gcd(x,y)=1. 考虑将图按对角线划分开,两部 ...