如何使用ajax调用将对象传递给控制器
问题描述:
我想将对象传递给控制器并检索控制器中的值。 我定义了诸如:如何使用ajax调用将对象传递给控制器
HTML代码:
var positionarray = [];
的Javascript:
$("#button").live('click',function(){
positionarray.push({
id: sessionStorage.getItem('id'),
value: $("#input").val()
});
});
// on save button click
$.ajax({
type: "GET",
url:"/Bugs/Position",
data: {
array:positionarray
},
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (json) {
}
});
但我不能够在控制器中检索值。它变为空。
答
试试这个: - 你传入对象的数组,所以你应该做的HTTPPost而非HTTPGET(这会为基本类型的数组工作说INT的名单,strgin等)通过查询字符串发送它(记住查询字符串限制)。 用HTTPPost试试这个
$.ajax({
type: "POST",
url:"Home/Position",
data: JSON.stringify({
array: positionarray
}),
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (json) {
}
[HTTPPost]
public void Position(YourClass[] array){...
答
尝试了这一点:
$.ajax({
type: "GET",
url:"/Bugs/Position",
data: 'array='+positionarray
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (json) {
}
});
答
试试这个:
你ajax call
必须在按钮的点击函数,那么它会工作,
在你的代码,你的前点击你的ajax call
运行,从而它将通过null 到您的控制器
$("#button").live('click',function(){
positionarray.push({
id: sessionStorage.getItem('id'),
value: $("#input").val()
});
// on save button click
// this code must run after `button click` and after pushing data in
// positionarray variable
$.ajax({
type: "GET",
url:"/Bugs/Position",
data: {
array:positionarray
},
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (json) {
}
});// here ajax function code ends
});// here button click function ends
你的动作是怎样的? – PSL 2013-04-29 04:58:12
在'/ Bugs/Position'页打印你的'Request',并检查你得到了什么,我认为它是正确的。 – 2013-04-29 04:59:47
我能够传递给控制器,但在控制器中获得的值为空 – Jonathan 2013-04-29 05:01:25