关于javascript:将JSON发送到WebMethod时出错

Error sending JSON to WebMethod

我无法将JSON发送到WebMethod。 这是我尝试这样做的方式。 如果有更好的方法,请告诉我。 我想要做的是将JSON对象保存在数据库中。

JavaScript的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function TEST() {
    var str = '[{
       "Key": 6311,
       "Start":"123 Start",
       "End":"456 End",
       "Date":"2/2/2012",
       "Order": null,
       "EstMiles": 0,
       "Stops": [
            {"StopAddy":"123 Stop Addy"},
            {"StopAddy":"456 Stop Addy"},
            {"StopAddy":"789 Stop Addy"}
        ]
    }]'
;    // Whitespace added for clarity

    $.ajax({
        type:"POST",
        url:"WebService1.asmx/Test",
        data: str,
        //contentType:"plain/text",
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success: function(msg) {
            alert(msg.d);
        },
        error: function(e) {
            alert(e.responseText);
        }
    });
}

VB.NET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Public Function Test(ByVal o As Object()) As String
    'do your processing  
    Return"success"
End Function

Public Class [Stop]
    Public Property StopAddy() As String
        Get
            Return m_StopAddy
        End Get
        Set(ByVal value As String)
            m_StopAddy = value
        End Set
    End Property
    Private m_StopAddy As String
End Class

Public Class RootObject
    Public Property Key() As Integer
        Get
            Return m_Key
        End Get
        Set(ByVal value As Integer)
            m_Key = value
        End Set
    End Property
    Private m_Key As Integer
    Public Property Start() As String
        Get
            Return m_Start
        End Get
        Set(ByVal value As String)
            m_Start = value
        End Set
    End Property
    Private m_Start As String
    Public Property [End]() As String
        Get
            Return m_End
        End Get
        Set(ByVal value As String)
            m_End = value
        End Set
    End Property
    Private m_End As String
    Public Property [Date]() As String
        Get
            Return m_Date
        End Get
        Set(ByVal value As String)
            m_Date = value
        End Set
    End Property
    Private m_Date As String
    Public Property Order() As Object
        Get
            Return m_Order
        End Get
        Set(ByVal value As Object)
            m_Order = value
        End Set
    End Property
    Private m_Order As Object
    Public Property EstMiles() As Integer
        Get
            Return m_EstMiles
        End Get
        Set(ByVal value As Integer)
            m_EstMiles = value
        End Set
    End Property
    Private m_EstMiles As Integer
    Public Property Stops() As List(Of [Stop])
        Get
            Return m_Stops
        End Get
        Set(ByVal value As List(Of [Stop]))
            m_Stops = value
        End Set
    End Property
    Private m_Stops As List(Of [Stop])
End Class

我得到的错误是

{"Message":"Type \u0027System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.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& 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.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)

   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)",
"ExceptionType":"System.InvalidOperationException"}


基于您的堆栈跟踪,我假设您在服务器端使用Asp.net?
如果是这样,我相信Asp.net要求你有一个主对象包装json数据的对象列表。 因此,请将您的输入更改为以下内容,并稍微更改您的服务器代码以处理额外的包装层:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var str = '{
"data":
[
    {
       "Key": 6311,
       "Start":"123 Start",
       "End":"456 End",
       "Date":"2/2/2012",
       "Order": null,
       "EstMiles": 0,
       "Stops": [
            {
               "StopAddy":"123 Stop Addy"
            },
            {
               "StopAddy":"456 Stop Addy"
            },
            {
               "StopAddy":"789 Stop Addy"
            }
        ]
    }
]
}'