I have been trying to implement th开发者_如何学Pythone jQuery weekcalendar using .net. What I can't seem to figure out is why weekcalendar states events.events is undefined after I make an ajax call to a webmethod I created which returns JSON.
Below is the relevant code:
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;
}
});
}
JSON directly from the webmethod (Also, if you're looking at that Date, before you comment read the above javascript that fixes it)
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
The success
member in your $.ajax
call is an anonymous function. It is executed as a callback of the asynchronous request and does not return jsonObj
to the caller of getEventData().
What you need to do instead is either call a function to make use of jsonObj
or process the object there, i.e.:
success: function (data) {
...
ProcessEventData(jsonObj);
}
精彩评论