我希望将一个对象传递给控制器并检索控制器中的值。我的定义如下:
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) {
}
});但是我无法检索控制器中的值。它正在变为null。
发布于 2013-04-29 13:23:53
尝试这样做:-你正在传递一个对象数组,所以你应该使用HTTPPost而不是HttpGet (这将适用于基元类型的数组,比如list of 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){...发布于 2013-04-29 12:57:16
试试这个:
$.ajax({
type: "GET",
url:"/Bugs/Position",
data: 'array='+positionarray
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (json) {
}
});发布于 2013-04-29 13:18:25
试试这个:
你的ajax call必须在按钮点击功能中,然后它才能工作,
在您的代码中,ajax call在单击之前运行,因此它会将空传递给控制器
$("#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 endshttps://stackoverflow.com/questions/16271404
复制相似问题