.NET序列化和weekcalendar - 未定义的events.events

时间:2011-10-13 18:58:27

标签: jquery vb.net json calendar

我一直在尝试使用.net实现jQuery weekcalendar。我似乎无法弄清楚为什么在我创建一个返回JSON的web方法的ajax调用之后,为什么weekcalendar声明events.events未定义。

以下是相关代码:

使用Javascript:

function getEventData() {
    var dataSource = $('#ddlAdvisors').val();

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: "{ AdvisorID: '" + $('#ddlAdvisors').val() + "'}",
        url: "<page>.aspx/<webmethod>",
        dataType: "json",
        success: function (data) {
            var jsonObj = $.parseJSON(data.d);

            // Fixes datetime

            $.each(jsonObj.events, function (key, value) {
                value.start = eval(value.start.slice(1, -1))
                value.end = eval(value.end.slice(1, -1))
            });

        return jsonObj;
        }
    });
}

直接来自webmethod的JSON(另外,如果您正在查看该日期,请在评论之前阅读以上修复它的javascript)

d = { "events" : [{
        "id":68263,
        "start":"\/Date(1262619000000)\/",
        "end":"\/Date(1262622600000)\/",
        "comment":"..comment..",
        "title":"First Name Last Name"
    }]
}

的WebMethod

<WebMethod()> _
Public Shared Function <name>(ByVal AdvisorID As String) As String

    Dim lookup As New <namespace>.<class>
    Dim dt As New DataTable
    dt = lookup.<function>(Convert.ToInt16(AdvisorID))

    Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()

    Dim events As New Generic.Dictionary(Of String, Generic.Dictionary(Of String, List(Of Generic.Dictionary(Of String, Object))))
    Dim rows As New List(Of Dictionary(Of String, Object))
    Dim row As Dictionary(Of String, Object)

    For Each dr As DataRow In dt.Rows
        row = New Dictionary(Of String, Object)
        For Each col As DataColumn In dt.Columns
            row.Add(col.ColumnName, dr(col))
        Next
        rows.Add(row)
    Next

    member.Add("events", rows)
    events.Add("events", member)

    Return serializer.Serialize(member)

End Function

1 个答案:

答案 0 :(得分:0)

success来电中的$.ajax成员是匿名函数。它作为异步请求的回调执行,并且不会将jsonObj返回给getEventData().的调用者。您需要做的是调用函数来使用jsonObj或进程那里的对象,即:

success: function (data) {

    ...

    ProcessEventData(jsonObj);

}