方法一:
1.使用JSON.stringify 将数组对象转化成json字符串
var array = ["1", "2"]
$.ajax({
type : 'POST',
url: path + '/check/testPost',
contentType : "application/json" ,
data : JSON.stringify(array),
success : function(data) {
}
})
2.后台处理
@RequestMapping(value = "/testPost", method = {RequestMethod.POST})
public void testPost(@RequestBody String[] array) throws IOException {
for (String string : array) {
System.out.println(string)
}
return
}
方法二:
1.前端不做处理:
var array = ["1", "2"]
$.ajax({
type : 'POST',
url: path + '/check/testPost',
contentType: "application/x-www-form-urlencoded",
data: {"array": array},
success : function(data) {
}
})
2.后台处理
@RequestMapping(value = "/testPost", method = {RequestMethod.POST})
public void testPost(HttpServletRequest req) throws IOException {
String[] array = req.getParameterValues("array[]")
for (String string : array) {
System.out.println(string)
}
return
}
注:两种post请求的content-type不同。
原文链接:https://blog.csdn.net/zhaohuijiadelu/article/details/54408324
通过form 表单和隐藏域传值<script>
var fjmc=window.parent.document.all.fjmctext.value
document.getElementByIdx_x_x_x("aaa").value = fjmc
myForm.action="collectionSendToExcel.action"
myForm.submit()
</scritp>
<form action="#" id="myFormToExcel" name="myFormToExcel">
<input type="hidden" name="fjmc" id="aaa" />
<input type="text" name="action" />
<input type="submit" value="提交'>
</form>
通过Ajax传递:
function commit(){
var myMask = new Ext.LoadMask(Ext.getBody(), {msg: '正在提交数据,请稍后...'})
myMask.show()
Ext.Ajax.request({
form: 'myForm',
method: 'post',
success: function(response, option){
myMask.hide()
var results = Ext.util.JSON.decode(response.responseText)
var msgs = '【承兑保证操作成功!】'
switch(results.type){
case '1':
msgs = '【保证人账号不能为空,操作失败】'
break
case '2':
msgs = '【访问数据库发生异常,操作失败】'
break
}
Ext.Msg.show({
title:'信息提示',
modal:false,
msg: msgs,
buttons: Ext.Msg.OK,
fn:function(btn,text){
if(btn == 'ok'){
if(results.success){
comBack(g('SBillType').value)//返回列表
}
}
},
animEl: 'elId',
icon: Ext.MessageBox.INFO
})
},
failure: function(response,option){
myMask.hide()
Ext.MessageBox.alert("信息提示","【提交数据超时,请稍候再试...】")
}
})
}
js把值传到后台是靠action提交form的时候hidden标签实现的。比如有个form如下:
<form id="myform" action="queryUser.do" method="post">
<input type="hidden" id="val" name="val"/>
<input type="text" name="userId" name="userId">123456</input>
</form>
当action用js提交的时候:
var myVal=document.getElementById("userId").value
document.getElementById("val").value=myVal;
document.form[0].submit()
这样的话userId这个值就传到后台去了。