ajax-传数组给后台

前端之家收集整理的这篇文章主要介绍了ajax-传数组给后台前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
var ids = [1,2,3,4];
$.ajax({
 type: "POST",url: "${base}/xxx/xx?ids=" + ids,async: false,dataType: "json",success: function (data) {
  if (data.data) {
      alert(data.data); //ids;
  }
 }
}); 


//后台controller-springMVC
 @RequestMapping("/delete")
 @ResponseBody
  public ResponseEntity<JsonResult> xx(Long[] ids) { 
    System.out.pringln(Arrays.toString(ids));//[1,4,5]
    return ControllerHelper.returnJSONResult(true,"成功",ids);
}


package com.gt.util.web;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;

import com.gt.util.web.JsonResult.ErrorCode;

public class ControllerHelper {

	/**
	 * 导航到错误页面
	 * 
	 * @param model
	 * @param errorInfo
	 * @return
	 */
	public static String navErrorPage(Model model,String errorInfo) {
		model.addAttribute("errorinfo",errorInfo);
		return "error/error";
	}

	/**
	 * 返回标准JSON返回值
	 * 
	 * @param result
	 * @param message
	 * @return
	 */
	public static ResponseEntity<JsonResult> returnJSONResult(boolean result,String message) {
		return returnJSONResult(result,message,null);
	}

	/**
	 * 返回标准JSON返回值
	 * 
	 * @param result
	 * @param message
	 * @param data
	 * @return
	 */
	public static ResponseEntity<JsonResult> returnJSONResult(boolean result,String message,Object data) {
		return returnJSON(new JsonResult(result,data));
	}

	/**
	 * 返回标准JSON返回值
	 * 
	 * @param result
	 * @param errorCode
	 * @param message
	 * @param data
	 * @return
	 */
	public static ResponseEntity<JsonResult> returnJSONResult(boolean result,ErrorCode errorCode,errorCode,data));
	}

	/**
	 * 返回给定数据的JSON格式
	 * 
	 * @param data
	 * @return
	 */
	public static <T> ResponseEntity<T> returnJSON(T data) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.parseMediaType(MediaType.TEXT_PLAIN + "; charset=utf-8"));
		return new ResponseEntity<T>(data,headers,HttpStatus.OK);
	}

	/**
	 * 返回给定数据的STREAM格式(二进制)
	 * 
	 * @param data
	 * @return
	 */
	public static ResponseEntity<?> returnData(Object data) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<Object>(data,HttpStatus.OK);
	}

	public static ResponseEntity<?> returnBadRequestData() {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<Object>(null,HttpStatus.BAD_REQUEST);
	}

	public static ResponseEntity<JsonResult> returnJSONResult(boolean result,String message) {
		return returnJSON(new JsonResult(result,null));
	}

	/**
	 * 返回处理结果,自动封装错误提示
	 * 
	 * @param b
	 * @param javaCodeError
	 * @return
	 */
	public static ResponseEntity<JsonResult> returnJSONResult(ErrorCode errorCode,boolean result) {
		return returnJSON(new JsonResult(result,errorCode.getDisplay(),null));
	}
}

package com.gt.util.web;

/**
 * Json操作反馈对象
 * 
 * @author 
 *
 */
public class JsonResult {

	public JsonResult(boolean result) {
		this(result,null);
	}

	public JsonResult(boolean result,String message) {
		this(result,Object data) {
		this(result,null,data);
	}

	public JsonResult(boolean result,Object data) {
		this.result = result;
		this.message = message;
		this.data = data;
	}

	public JsonResult(boolean result,Object data) {
		this.result = result;
		if(errorCode!= null){
			this.errorCode = errorCode.getCode();
		}
		this.message = message;
		this.data = data;
	}

	/**
	 * 操作结果
	 */
	private boolean result;
	/**
	 * 错误代码,正确时使用0
	 */
	private int errorCode;
	private String message;
	private Object data;
	
	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public static JsonResult createFailureResult(String message) {
		return new JsonResult(false,message);
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public int getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(ErrorCode errorCode) {
		this.errorCode = errorCode.getCode();
	}

	public enum ErrorCode {
		/**OK = 200*/
		NONE("",200),/**您的登录状态已失效,请重新登录!= 401*/
		NOT_LOGIN("您的登录状态已失效,请重新登录!",401),/**系统未查询到该数据=4041*/
		NOT_FIND_DATA("系统未查询到该数据",4041),/**对不起,安全验证失败=402*/
        NOT_VALID("对不起,安全验证失败",402),/**可以自定义错误信息4021(如删除用户,如果该用户管理员,可以提是管理员不能被删除)***/
        CURRENT_NOT_PERMISS_OPERATE("当前状态下不容许该操作",4021),/**数据格式不符合要求,请重新提交! 400*/
        DATA_VALID_FAILURE("数据格式不符合要求,请重新提交!",400),/**网络出错,请重新操作!=505*/
		JAVA_CODE_ERROR("网络出错,请重新操作!",505),/**新增功能请下载最新版本*/
        BUSSINESS_NOT_SUPPORTED("当前客户端版本过低,不支持功能,请下载最新版本",600),/**对不起,您没有执行该操作的权限=403*/
		PERMISS_DENIED("对不起,您没有执行该操作的权限",403),/**对不起,您没有操作该数据的权限!=4032*/
		PERMISS_DATA_DENIED("对不起,您没有操作该数据的权限!",4032),/**您还没有注册公司,无法执行该操作=4031*/
		NOT_COMPANY("您还没有注册公司,无法执行该操作",4031),/**您当前无法参与报价=4032*/
        NOT_ALLOW_QUOTATION("您当前无法参与报价",COMPANY_INFO_UN_PERFECT("<b><u><font color='red'>企业注册信息不完整,请尽快补全:</font></u></b><br>1.请到 企业申请 中进行完善;<br>2.或者联系“企业管理员”进行完善。<br/>3.信息不完整不影响正常创建操作!",4033);

		private String display;
		private int code;

		// 构造方法
		private ErrorCode(String display,int code) {
			this.display = display;
			this.code = code;
		}

		public int getCode() {
			return this.code;
		}

		public String getValue() {
			return name();
		}

		public static ErrorCode fromCode(int code) {
			ErrorCode[] states = ErrorCode.values();
			for (ErrorCode state : states) {
				if (state.getCode() == code) {
					return state;
				}
			}
			return null;
		}

		// 覆盖方法
		@Override
		public String toString() {
			return this.display;
		}

		public String getDisplay() {
			return display;
		}
	}
}
原文链接:https://www.f2er.com/ajax/161552.html

猜你在找的Ajax相关文章