将Json传递给WebMethod

时间:2012-02-02 19:26:53

标签: javascript jquery asp.net json asmx

我在反序列化o时遇到错误。 o由许多汽车组成,我试图将它们分解为oCarsList。我试图将o更改为ByVal o As List(Of Cars)(),但是我收到了一个javascript错误。

的Javascript

var str2 = JSON.stringify({CarsObject: Cars});

    $.ajax({
        type: "POST",
        url: "wsCars.asmx/SetCars",
        //data: Cars,
        //data: {"Cars":Cars.toString()},
        data: "{o: " + str2 + "}",
        processData: false,
        //contentType: "plain/text",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert(msg.d);

        },
        error: function(e) {
            alert(e);
        }
    });

VB

    Public Function SetCars(ByVal o As Object) As String 
        If o.Count > 0 Then

            Dim oSerializer As New JavaScriptSerializer
            Dim oCarsList As New List(Of Cars)()

            Try
                oCarsList = oSerializer.DeserializeObject(o)
            Catch ex As Exception
            End Try
        End If
    End Function

1 个答案:

答案 0 :(得分:0)

我建议不要运行stringify,然后将JSON元素连接成字符串。

var str2 = JSON.stringify({o:{CarsObject: Cars}});