Ajax post 请求后端,server返回Map结果集

前端之家收集整理的这篇文章主要介绍了Ajax post 请求后端,server返回Map结果集前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、应用场景

当前端发出Ajax请求,后端需要返回Map结果集

二、代码

1、ResultFactory.java

package com.XXXX.tv.vrs.service.util;

import java.util.HashMap;
import java.util.Map;

public class ResultFactory {
	
	public static Map<String,Object> makeOkResult(Object data){
		Map<String,Object> res = new HashMap<String,Object>();
		res.put("status",StatusCode.OK);
		res.put("msg",StatusCode.OK_MSG);
		res.put("data",data);
		return res;
	}
	
	public static Map<String,Object> makeErrorResult(){
		Map<String,StatusCode.SERVER_ERROR);
		res.put("msg","Error");
		res.put("data",null);
		return res;
	}

}

2、controller

@RequestMapping("/uppdateSensitiveWord.do")
@ResponseBody
public Map<String,Object> updateSensitiveWord(String sensitiveInput,String sensitiveWordBeUpdate){

		Map<String,Object> map = new HashMap<String,Object>();
		try {
			Long rs=sensitiveWordService.updateSensitiveWord(sensitiveWordBeUpdate,sensitiveInput);
			map = ResultFactory.makeOkResult(rs);
		}catch(Exception e) {
			e.printStackTrace();
			map = ResultFactory.makeErrorResult();
		}	
		return map;
	}

3、JS

function updateSensitiveWord()  {  
			var sensitiveInput=$.trim($("#sensitive").val());
			var sensitiveWordBeUpdate=$.trim($("#sensitiveWordUpdate").val());
			//alert(sensitiveWordBeUpdate+"--->"+sensitiveInput);
			$.post("uppdateSensitiveWord.do",{sensitiveInput: sensitiveInput,sensitiveWordBeUpdate:sensitiveWordBeUpdate},function(rs){
					if(rs.status==200 &&rs.data!=0){
						alert("修改成功");
						window.location.reload();
					}else if(rs.status==200 &&rs.data==0){
						alert("数据库中已存在敏感词"+sensitiveInput+",修改失败");
					}else{
						alert("修改失败");
					}
			});
	    }
原文链接:https://www.f2er.com/ajax/160461.html

猜你在找的Ajax相关文章