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

    1.Mybatis-generator 自动化生成数据库交互代码->dao+pojo+xml 2.Mybatis-plugin dao文件和xml自动跳转,验证正确性,在xml中只能提示等功能 ...

  2. Tomcat下载安装及常见问题解决办法

    一.Tomcat的下载: 下载地址:http://tomcat.apache.org/ 下载Tomcat6.0(在左侧的Download下,考虑到稳定性现在企业大部分还在用Tomcat6.0) (1) ...

  3. mpvue-小程序之蹲坑记

    1. 不支持 v-html 小程序里所有的 BOM/DOM 都不能用,也就是说 v-html 指令不能用 部分复杂的 JavaScript 渲染表达式 {{}} 双花括号的部分,直接编码到 wxml ...

  4. MySQL 的七种 join

    建表 在这里呢我们先来建立两张有外键关联的张表. CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept`( `id` ...

  5. 超详细saltstack安装部署及应用

    1.环境准备 准备两台虚拟机 主机名 ip role linux-node1 10.0.0.7 master linux-node2 10.0.0.8 minion 在节点1上安装 master 和 ...

  6. 第三篇:使用firewall-cmd

    本文来源:Working with firewalld         其它参考文档:redhat官方中文文档 1.查询类命令 2.设置类命令 3.运行时zone设置 4.永久性zone设置 5.直接 ...

  7. TS 数据流分析学习

    TS 流.包结构以及同步 1. TS 流: 可以将TS流理解为一种单一码流.混合码流. 单一码流:TS流的基本组成单位是长度为188字节的TS包. 混合码流:TS流有多种数据组成,一个TS包中的数据可 ...

  8. spring applicationContext.xml 中bean配置

    如果采用set get方法配置bean,bean需要有set get 方法需要有无参构造函数,spring 在生成对象时候会调用get和set方法还有无参构造函数 如果采用constructor方法则 ...

  9. go语言多态接口样例

    感觉比java玄幻啊~~~ package main import ( "fmt" ) type notifier interface{ notify() } type user ...

  10. 使用navicat修改Mysql默认密码

    本质就是修改数据库mysql里面的数据表user一个字段,并执行如下一句mysql语句: UPDATE user SET password=PASSWORD('密码') WHERE user='roo ...