我试图通过几个简单的例子来学习jQuery和JSON,这让我很难过。
我有一个按钮,单击时我想将JSON对象传递给WebMethod,对它执行一些字符串操作并返回到客户端。但是当我点击按钮时没有任何反应。
我已经为web方法添加了一个断点,但是从未进入过,这就像从未调用过webmethod一样。
检查浏览器控制台我可以看到webmethod下面出现System.InvalidOperationException
错误(下面的完整错误消息)。
非常感谢任何帮助。
要传递给网络方法的数据
"id": "1", "name": "Zippy"
"id": "2", "name": "George"
"id": "3", "name": "Bungle"
"id": "4", "name": "geoffrey"
的jQuery
$('#SaveButton').click(function () {
var jsonObj = [{ "id": "1", "name": "Zippy" }, { "id": "2", "name": "George" }, { "id": "3", "name": "Bungle" }, { "id": "4", "name": "geoffrey"}]
jsonObj = JSON.stringify(jsonObj);
SaveNewOrder(jsonObj);
});
function SaveNewOrder(jsonObj) {
$.ajax({
type: "POST",
url: "ColumnSetting.aspx/setrecord",
data: jsonObj,
contentType: "application/json; charset=utf-8",
dataType: "html",
success: OnSuccess,
failure: OnError
});
}
function OnSuccess(response) {
$('#OutputDiv').html(response.d);
}
function OnError(response) {
$('#OutputDiv').html(response.d);
}
网络方法和课程
public class ListOfDestinationColumns
{
public List<DestinationColumn> data { get; set; }
}
public class DestinationColumn
{
public int id { get; set; }
public string name { get; set; }
}
[WebMethod]
public static string setrecord(string jsonObj)
{
ListOfDestinationColumns Destinations = new JavaScriptSerializer().Deserialize<ListOfDestinationColumns>(jsonObj);
string output = "Start<br/>";
foreach (var item in Destinations.data)
{
output += string.Format("id: {0}, name: {1}<br />", item.id.ToString(), item.name);
}
output += "End";
return output;
}
浏览器控制台中的错误消息
ExceptionType:"System.InvalidOperationException"
Message:"Type 'System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' is not supported for deserialization of an array."
StackTrace:" at System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer serializer, Boolean throwOnError, IList& convertedList)
↵ at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
↵ at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
↵ at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
↵ at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
↵ at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)
↵ at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)
↵ at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)"
答案 0 :(得分:0)
JavaScriptSerializer不会反序列化字典对象,特别是如果您有任何复杂的类型和日期。
您收到如下错误,
键入&#39; System.Collections.Generic.IDictionary`2 [[System.String, mscorlib,版本= 4.0.0.0,文化=中性, PublicKeyToken = b77a5c561934e089],[System.Object,mscorlib, Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]&#39; 不支持反序列化数组。
您可以使用Newtonsoft.Json解决此问题。
您可以从以下链接下载Json.NET 9.0.1(此版本)。
https://www.nuget.org/packages/Newtonsoft.Json/
希望这有帮助!