How can I retrieve JSONdata from ASP.NET and send it to jQuery? (implementing FullCalendar)

感情迁移 提交于 2020-01-04 09:26:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!