我在使用 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深度限制,JavaScriptSerializer序列化器)
在System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer序列化器,字符串输入,类型类型,Int32深度限制)
在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](字符串输入)
在 System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext 上下文,JavaScriptSerializer 序列化器)
在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext 上下文)
在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}"