Ajax および ASP.NET WebMethods を使用して JSON オブジェクトを渡す際に問題があります。
function setStudentInfo() {
var jsonObjects = [
{ id: 1, name: "mike" },
{ id: 2, name: "kile" },
{ id: 3, name: "brian" },
{ id: 1, name: "tom" }
];
$.ajax({
type: "POST",
url: "ConfigureManager.aspx/SetStudentInfo",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
data: { students: JSON.stringify(jsonObjects) },
success: function (result) {
alert('success');
},
error: function (result) {
alert(result.responseText);
}
});
}
ASP.NET コード
[WebMethod]
public static void SetStudentInfo(object students)
{
//Here I want to iterate the 4 objects and to print their name and id
}
次のエラーが表示されます。
"{"Message":"無効な JSON プリミティブ: 学生。"、"StackTrace":" System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() で System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 深度) System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize (文字列入力、Int32 depthLimit、JavaScriptSerializer シリアライザー) で System.Web.Script.Serialization.JavaScriptSerializer.Deserialize (JavaScriptSerializer シリアライザー、文字列入力、型の種類、Int32 depthLimit) システムで。 System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext コンテキスト、JavaScriptSerializer シリアライザー) で System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext コンテキスト) で System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext コンテキスト, WebServiceMethodData methodData)","ExceptionType":"System. ArgumentException"}"