ajax 200不走succes 已解决!!!

记录一个今天遇到的问题,ajax前后台交互完直接打开了一个新页面并且将返回值打印出来。
先看下图
在这里插入图片描述
之前的代码

$.ajax({
          url : "***.do?***_cd="+$("#***_cd").val(),
            type : "post",
            async : false,
            dataType : "json",
            success : function(result) {
            	console.log("+++")
            	console.log(result);
            }
  });

然后找错误=====>加一个err

$.ajax({
           url : "***.do?***_cd="+$("#***_cd").val(),
           type : "post",
           async : false,
           dataType : "json",
           success : function(result) {
           	console.log("+++")
           	console.log(result);
           },
       	error : function(errorMsg) {
       		console.log("----");
       		console.log(errorMsg);
          	}	
    });

可以看到 走了err,并且状态是200
在这里插入图片描述
找资料 找方法
https://blog.csdn.net/cl598598598/article/details/95600187
https://blog.csdn.net/LJFPHP/article/details/87874692

找到问题=====》后台返回的数据类型和jsonType设置的类型不一致

方法======》改变ajax的dataType json改text

$.ajax({
        url : "***.do?***_cd="+$("#***_cd").val(),
           type : "post",
           async : false,
           dataType : "text",
           success : function(result) {
           	console.log("+++")
           	console.log(result);
        },
       	error : function(errorMsg) {
       		console.log("----");
       		console.log(errorMsg);
        }	
});

此时可以看到走了success 但返回格式不是我想要的
在这里插入图片描述
再次改变后台代码

JSONObject list = new JSONObject();
JSONArray ship_portBox_List = dao.selectBoxReturnJsonArray(userEntity.getBaseCd(), userEntity.getLanguage(),
		Constant.DB_ITEM_NKBN_SHIPPORT,shiiresaki_cd, true);
list.put("ship_portBox_List", ship_portBox_List);
public JSONArray selectBoxReturnJsonArray(String baseCd, String langCd, String nkbn, String code,boolean hasEmpVal) throws Exception {
		
		//返却用リストの初期化
		JSONArray resultSet = new JSONArray();
		
		sql片段********
		
		// sql返回结果
		List<HashMap<String, Object>> resultRecList = 
				super.getDbAccessService().executeQuery(sqlSb.toString(), paramList);
		
			if (hasEmpVal) {
				JSONObject data = new JSONObject();
				data.put("cd", Constant.PG_BASIC_STRING_SPACING);
				data.put("nm", Constant.PG_BASIC_STRING_SPACING);
				resultSet.put(data);
			}
			
			for (HashMap<String, Object> resultRecMap : resultRecList) {
				JSONObject data = new JSONObject();
				data.put("cd", StringUtil.changeHTMLEncode((String) resultRecMap.get("cd")));
				data.put("nm",StringUtil.changeHTMLEncode((String) resultRecMap.get("nm")));
				resultSet.put(data);
			}
		return resultSet;
	}

然后再将jsonType的类型改为 json即可 到此结束!!!
最终返回图
在这里插入图片描述
end

猜你喜欢

转载自blog.csdn.net/c15162/article/details/117251265
今日推荐