【问题标题】:Pass JSON to aspx webmethod gives System.InvalidOperationException error将 JSON 传递给 aspx webmethod 给出 System.InvalidOperationException 错误
【发布时间】:2017-08-01 16:10:30
【问题描述】:

我正在尝试通过几个简单的示例来学习 jQuery 和 JSON,这让我很困惑。

我有一个按钮,单击该按钮后,我想将 JSON 对象传递给 WebMethod,对其执行一些字符串操作并返回到客户端。但是,当我单击按钮时,什么也没有发生。

我在 webmethod 中添加了一个断点,但是从来没有进入,就像 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);
}

Web 方法和类

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)"

【问题讨论】:

    标签: c# jquery asp.net json ajax


    【解决方案1】:

    JavaScriptSerializer 不会反序列化字典对象,尤其是当您有任何复杂的类型和日期时。

    你得到如下错误,

    键入'System.Collections.Generic.IDictionary`2[[System.String, mscorlib,版本=4.0.0.0,文化=中性, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, 版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]]' 不支持数组的反序列化。

    您可以使用 Newtonsoft.Json 解决此问题。

    您可以从以下链接下载Json.NET 9.0.1(此版本)。

    https://www.nuget.org/packages/Newtonsoft.Json/

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 2013-05-14
      • 2017-03-02
      相关资源
      最近更新 更多