admin管理员组文章数量:1415109
What should the WebMethod's parameter be called below to get the json array sent from client? I used name
, but it didn't work.
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);
This is the jquery ajax to send it to Web Method.
$("#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");
}
});
And this is the Web Method
[System.Web.Services.WebMethod]
public static string GetDetails(string name)
{
var x=name;
return "";
}
What should the WebMethod's parameter be called below to get the json array sent from client? I used name
, but it didn't work.
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);
This is the jquery ajax to send it to Web Method.
$("#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");
}
});
And this is the Web Method
[System.Web.Services.WebMethod]
public static string GetDetails(string name)
{
var x=name;
return "";
}
Share
Improve this question
asked Sep 18, 2014 at 17:48
JudeJude
2,43311 gold badges50 silver badges71 bronze badges
1
-
1
If you tell the server that you are sending JSON, you actually have to send JSON.
'{name: "' + toServer + '" }'
is not JSON. Maybe do onlydata: toServer
instead. – Felix Kling Commented Sep 18, 2014 at 17:50
2 Answers
Reset to default 3You have to rewrite your data initialization:
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");
}
use object parameter type on server side:
[System.Web.Services.WebMethod]
public static string GetDetails(object name)
{
var x=name;
return "";
}
Edit: The quotes removal is not needed as @Felix Kling pointed out.
You have to simply change your web method to:
[System.Web.Services.WebMethod]
public static string GetDetails(object name)
{
var x=name;
return "";
}
本文标签: javascriptSending array of Json objects to Web Method using JQuery AjaxStack Overflow
版权声明:本文标题:javascript - Sending array of Json objects to Web Method using JQuery Ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745228677a2648726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论