问题
I'm trying to get JSON-data into a jQuery variable using ASP.NET (not MVC):
$(document).ready(function () {
$('#calendar').fullCalendar({
events: GetEvents(start, end) //This line is invalid
}
}
In MVC, it could just be events: "/Calendar/GetEvents/"
, which would call the CalendarController's GetEvents()-method.
But since I'm not using MVC I started following this guide to try calling server-side methods from the client.
In the second step it tells me that I have to create a static method in order to do this:
[System.Web.Services.WebMethod]
public static string Message()
{
return "Hello from the server-side World!";
}
But I need to be able to access non-static variables like Session[]
inside the method, so I can't really see how this approach would work.
Is there a better approach to getting JSON-data extracted from an aspx.cs-method that doesn't involve making direct server-side calls? Or is there a way for me to use the Session that I'm not aware of?
回答1:
Instead of calling Session directly use HttpContext.Current.Session, or make it a static property in your page:
private static HttpSessionState MySession
{
get
{
return HttpContext.Current.Session;
}
set
{
return HttpContext.Current.Session = value;
}
}
来源:https://stackoverflow.com/questions/12195445/how-can-i-retrieve-jsondata-from-asp-net-and-send-it-to-jquery-implementing-fu