调试代码遇到一个问题,就是前台运行删除操作后,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. APMserv常见问题

    一.启动是提示apache启动失败,mysql启动成功 1.去掉ssl的勾选(勾选需要证书),重启看解决没有 2.端口被占,吧apache端口改成80或者8080端口(其他端口也可以试试,80的话访问 ...

  2. tushrea知识笔记

    生成时间序列: dates = pandas.date_range('2013-01-01',periods = 6) Pandas读取excel数据: df=pd.read_excel(" ...

  3. Python垃圾回收机制:gc模块(zz)

    在Python中,为了解决内存泄露问题,采用了对象引用计数,并基于引用计数实现自动垃圾回收. 由于Python 有了自动垃圾回收功能,就造成了不少初学者误认为不必再受内存泄漏的骚扰了.但如果仔细查看一 ...

  4. ros 如何使用 openni2_launch

    There is very little actual code/etc in openni2_launch, it is mostly a thin wrapper around openni2_c ...

  5. Tomcat的类加载机制

     一个功能健全的Web服务器,要解决如下几个问题: 部署在同一个服务器上的两个Web应用程序使用的Java 类库可以实现相互隔离.不能要求一个类库在一个服务器中只有一份,服务器应当保证两个应用程序的类 ...

  6. [BZOJ1491][NOI2007]社交网络 floyd

    1491: [NOI2007]社交网络 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2196  Solved: 1170[Submit][Status ...

  7. 蓝牙遥控小车设计(三)——Amarino和 Android手机重力感应控制

    最近事真是多啊,一件接着一件的,加上自己拖延症~ - -! 遥控小车基本完成了,只是自己没时间来更新. 现在更新手机控制的部分 首先我们要熟悉一个软件—— 官网地址:http://www.amarin ...

  8. EasyUI----DataGrid行明细增删改操作

    http://blog.csdn.net/huchiwei/article/details/7787947   本文实现的是EasyUI-DataGrid行明细的增删改操作.具体参考来自以下文章: 官 ...

  9. .NET Core CLI

    NET Core 命令 一. 帮助命令 dotnet help 使用情况: dotnet [sdk-options] [command] [command-options] [arguments] 执 ...

  10. opencv中CV_IMAGE_ELEM的用法读取每个像素

    可以使用OpenCV定义的宏来提取象素值假设灰度图像image,存取其i行j列的象素可以这样:CV_IMAGE_ELEM(image, uchar,y, x)如果是彩色图像就是CV_IMAGE_ELE ...