• ajax不运行success回调而是运行error回调


      

           调试代码遇到一个问题,就是前台运行删除操作后,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");
    								}
    						}						
    
    					});
    				}
    			});
    		}




  • 相关阅读:
    Notepad++如何对比文件 Notepad++对比两个文件代码方法
    如何识别图片中的文字
    如何用DOS命令查看占用某端口的程序及PID号
    java使用POI获取sheet、行数、列数
    程序中的.htaccess文件是做什么的
    阿里云服务器配置https(总结)
    legend3---19、要更多的从服务器端控制元素的显示和隐藏,而不要是页面端
    Laravel 中 Session 的使用问题(dd()导致laravel中session取值问题)
    legend3---lamp.sh常用操作
    阿里云服务器发送邮件:Connection could not be established with host smtp.qq.com [Connection timed out #110]
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6747447.html
Copyright © 2020-2023  润新知