Skip to content

Instantly share code, notes, and snippets.

@xcaspar
Created July 21, 2015 07:35
Show Gist options
  • Save xcaspar/87d2a9ceeaf2e67d2b17 to your computer and use it in GitHub Desktop.
Save xcaspar/87d2a9ceeaf2e67d2b17 to your computer and use it in GitHub Desktop.

JS数组往后台传参问题

@(Spring)[javascript]

问题

在项目开发中,要将一个数组数据,通过js ajax 传到后台,例如:js代码:

var arr = [];
arr.push({
	  “key”:”a”,
	  “value”: “b”
	},….);

通过ajax传到后台:

$.ajax({
   url: ….,
   type: “post”,
   data: {
   arr: arr
}
})

后台代码如下:

@RequestMapping(value=”…”) 
public String getXXX(Model model, String[] arr) {
	…
}

但是后台 arr接收到的数据是null,

解决方法

attr.push(JSON.stringify({“key”:”a”,”value”:”b”})) 往后台传: data:{ arr:arr.toString() } 后台已字符串接收即可。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment