JSON Stringify不在数组上工作(ASP.NET)

时间:2017-12-29 19:56:08

标签: javascript jquery asp.net json ajax

EDIT2:找到解决方案,请参阅JSON, AJAX, and ASP.NET

问题最终是ajax调用中的参数不作为字符串发送到服务器端的WebMethod。 ajax调用的数据字段中的JSON字符串实际上转换为WebMethod参数中指定的对象类型。话虽这么说,我的问题是数据没有作为字符串传递给WebMethod,而是一种不同的对象类型。

编辑:查看Chrome中的浏览器控制台,我收到此错误:无法加载资源:服务器响应状态为500(内部服务器错误)。深入挖掘,我有以下回应:

{"Message":"Type \u0027System.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]]\u0027 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\u0026 convertedList)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

以下的ajax调用永远不会执行,似乎数据参数没有有效值。如果将data设置为等于具有有效JSON的字符串文字,则没有问题。在ajax调用中,似乎存在将javascript对象转换为data的有效参数的问题。请参阅我的aspx文件中的以下代码片段:

<script type="text/javascript">
        $(document).ready((function () {
            $('#mytable tr').click(function (event) {
                var array = [];
                $(".tdval").each(function () { 
                    array.push({
                        value: $(this).html()
                    });
                });
                alert(JSON.stringify(array)); //produces what appears to be valid json after calling stringify: [{"value":"val1"},{"value":"val2"}]

                $.ajax({
                    type: "POST",
                    url: "schedule.aspx/GetCurrentTime",
                    data: JSON.stringify({ array }), //this seems to be the issue
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response){
                        alert(response.d + "lol");
                    },
                    complete: function () {
                        alert("complete");
                    }
                });
            });
        }));
    </script>

这是ajax调用后面的C#代码中的方法:

    [System.Web.Services.WebMethod]
    public static string GetCurrentTime(string name)
    {
        return "Hello ";
    }

有趣的是,我能够将ajax调用中的data: JSON.stringify({ array })更改为一些随机json作为字符串文字,并且它可以正常工作。但是当使用stringify时,从不执行ajax调用的“success”回调方法。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

当您调用y = rand时,您在对象中有数组,因此您发送的JSON将如下所示:

JSON.stringify()

而不是你之前在函数中提醒的内容。

相反,请使用:

{ array: [{"value":"val1"},{"value":"val2"}] }