Javascript 使用JQuery Ajax将Json对象数组发送到Web方法
下面应该调用什么WebMethod的参数来获取从客户端发送的json数组?我使用了Javascript 使用JQuery Ajax将Json对象数组发送到Web方法,javascript,jquery,asp.net,ajax,json,Javascript,Jquery,Asp.net,Ajax,Json,下面应该调用什么WebMethod的参数来获取从客户端发送的json数组?我使用了名称,但它不起作用 var employees = { "accounting": [ // accounting is an array in employees. { "firstName": "", // First element "lastName":
名称
,但它不起作用
var employees = {
"accounting": [ // accounting is an array in employees.
{
"firstName": "", // First element
"lastName": "Doe",
"age": 23
},
{
"firstName": "Mary", // Second Element
"lastName": "Smith",
"age": 32
}
], // End "accounting" array.
"sales": [ // Sales is another array in employees.
{
"firstName": "Sally", // First Element
"lastName": "Green",
"age": 27
},
{
"firstName": "Jim", // Second Element
"lastName": "Galley",
"age": 41
}
] // End "sales" Array.
} // End Employees
var toServer = JSON.stringify(employees);
这是jqueryajax将其发送到Web的方法
$("#sendtoServer").click(function () {
$.ajax({
type : "POST",
url : "Default.aspx/GetDetails",
data : '{name: "' + toServer + '" }',
contentType : "application/json; charset=utf-8",
dataType : "json",
success : OnSuccess,
failure : function (response) {
alert("Wrong");
}
});
function OnSuccess(response) {
alert("Sent");
}
});
这就是Web方法
[System.Web.Services.WebMethod]
public static string GetDetails(string name)
{
var x=name;
return "";
}
您只需将web方法更改为:
[System.Web.Services.WebMethod]
public static string GetDetails(object name)
{
var x=name;
return "";
}
您只需将web方法更改为:
[System.Web.Services.WebMethod]
public static string GetDetails(object name)
{
var x=name;
return "";
}
您必须重写数据初始化:
var employees = {
accounting: [ // accounting is an array in employees.
{
firstName: "", // First element
lastName: "Doe",
age: 23
},
{
firstName: "Mary", // Second Element
lastName: "Smith",
age: 32
}
], // End "accounting" array.
sales: [ // Sales is another array in employees.
{
firstName: "Sally", // First Element
lastName: "Green",
age: 27
},
{
firstName: "Jim", // Second Element
lastName: "Galley",
age: 41
}
] // End "sales" Array.
} // End Employees
$.ajax({
type: "POST",
url: "Default.aspx/GetDetails",
data: JSON.stringify({ name: employees }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert("Wrong");
}
});
function OnSuccess(response) {
alert("Sent");
}
在服务器端使用对象参数类型:
[System.Web.Services.WebMethod]
public static string GetDetails(object name)
{
var x=name;
return "";
}
编辑:
正如@Felix Kling指出的,不需要删除引号。您必须重写数据初始化:
var employees = {
accounting: [ // accounting is an array in employees.
{
firstName: "", // First element
lastName: "Doe",
age: 23
},
{
firstName: "Mary", // Second Element
lastName: "Smith",
age: 32
}
], // End "accounting" array.
sales: [ // Sales is another array in employees.
{
firstName: "Sally", // First Element
lastName: "Green",
age: 27
},
{
firstName: "Jim", // Second Element
lastName: "Galley",
age: 41
}
] // End "sales" Array.
} // End Employees
$.ajax({
type: "POST",
url: "Default.aspx/GetDetails",
data: JSON.stringify({ name: employees }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert("Wrong");
}
});
function OnSuccess(response) {
alert("Sent");
}
在服务器端使用对象参数类型:
[System.Web.Services.WebMethod]
public static string GetDetails(object name)
{
var x=name;
return "";
}
编辑:
正如@Felix Kling指出的那样,不需要删除引号。如果你告诉服务器你正在发送JSON,你实际上必须发送JSON<代码>'{name:'+toServer+''''''}'不是JSON。可能只做
data:toServer
。如果你告诉服务器你正在发送JSON,你实际上必须发送JSON<代码>'{name:'+toServer+''''''}'不是JSON。可能只做data:toServer
。工作得很有魅力@裘德:请注意,没有必要删除物业名称中的引号。唯一重要的区别是data:JSON.stringify({name:employees})
和GetDetails(objectname)
。虽然data:JSON.stringify(employees)
也足够了,但不需要向object-structure.com添加额外的级别。工作得很有魅力@裘德:请注意,没有必要删除物业名称中的引号。唯一重要的区别是data:JSON.stringify({name:employees})
和GetDetails(objectname)
。虽然data:JSON.stringify(employees)
也足够了,但不需要向对象结构添加额外的级别。