2012-09-09 136 views
1

作爲一羣其他人,我有一個問題讓我的JSON供稿事件在日曆中呈現。這個問題通常是錯誤的JSON格式化,但事實並非如此,因爲我已經使用JSONlint對其進行了驗證,並且對Site.Master中的JSON提要進行了硬編碼,並取得了積極的結果。fullCalendar事件即使正確的JSON供稿不會顯示

FireBug獲得了正確的JSON響應,但它仍然沒有顯示在fullCalendar中。我沒有想法。螢火蟲迴應: [{「id」:1,「title」:「TESTTITLE」,「info」:「INFOINFOINFO」,「start」:「2012-08-20T12:00:00」,「端 「:」 2012-08-20T12:00:00" , 「用戶」:1}]

JSON.aspx

public partial class JSON : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    // Get events from db and add to list. 
    DataClassesDataContext db = new DataClassesDataContext(); 
    List<calevent> eventList = db.calevents.ToList(); 

    // Select events and return datetime as sortable XML Schema style. 
    var events = from ev in eventList 
       select new 
       { 
        id = ev.event_id, 
        title = ev.title, 
        info = ev.description, 
        start = ev.event_start.ToString("s"), 
        end = ev.event_end.ToString("s"), 
        user = ev.user_id 
       }; 

    // Serialize to JSON string. 
    JavaScriptSerializer jss = new JavaScriptSerializer(); 
    String json = jss.Serialize(events); 

    Response.Write(json); 
    Response.End(); 
    } 
} 

的Site.Master

<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />  
<link href='fullcalendar/fullcalendar.css' rel='stylesheet' type='text/css' /> 
<script src='jquery/jquery-1.7.1.min.js' type='text/javascript'></script> 
<script src='fullcalendar/fullcalendar.js' type='text/javascript' ></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#fullcal').fullCalendar({ 

      eventClick: function() { 
       alert('a day has been clicked!'); 
      }, 
      events: 'JSON.aspx' 
     }) 
    }); 
</script> 

我已經掃描有關天的相關問題,但沒有一個似乎修復我的...

+0

可能重複(已編輯)](http://stackoverflow.com/questions/12339508/fullcalendar-fetching-json-feededited) –

+0

那不是你如何填寫ajax –

+0

是LB這是一個更新。我無法弄清楚如何刪除它。 – Mix

回答

2

試試這個,你必須有在aspx文件一個WebMethod是fullcalendar可以異步調用

 $(document).ready(function() { 
     $('#fullcal').fullCalendar({ 
     eventClick: function() { 
      alert('a day has been clicked!'); 
     }, 
      events: function (start, end, callback) { 
      $.ajax({ 
       type: "POST", //WebMethods will not allow GET 
       url: "json.aspx/GetEvents", //url of a webmethod - example below 
       data: "{'userID':'" + $('#<%= hidUserID.ClientID %>').val() + "'}", //this is what I use to pass who's calendar it is 
       //completely take out 'data:' line if you don't want to pass to webmethod - Important to also change webmethod to not accept any parameters 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (doc) { 
        var events = []; //javascript event object created here 
        var obj = $.parseJSON(doc.d); //.net returns json wrapped in "d" 
        $(obj.event).each(function() { //yours is obj.calevent       
          events.push({ 
          title: $(this).attr('title'), //your calevent object has identical parameters 'title', 'start', ect, so this will work 
          start: $(this).attr('start'), // will be parsed into DateTime object  
          end: $(this).attr('end'), 
          id: $(this).attr('id') 
         }); 
        });      
        callback(events); 
       } 
      }); 
     } 
     }) 

那麼這將是json.aspx

[WebMethod(EnableSession = true)] 
public static string GetEvents(string userID) 
{ 
    DataClassesDataContext db = new DataClassesDataContext(); 
List<calevent> eventList = db.calevents.ToList(); 

// Select events and return datetime as sortable XML Schema style. 
var events = from ev in eventList 
      select new 
      { 
       id = ev.event_id, 
       title = ev.title, 
       info = ev.description, 
       start = ev.event_start.ToString("s"), 
       end = ev.event_end.ToString("s"), 
       user = ev.user_id 
      }; 

// Serialize to JSON string. 
JavaScriptSerializer jss = new JavaScriptSerializer(); 
String json = jss.Serialize(events); 
return json; 
} 
[Fullcalendar /擷取JSON飼料的
+0

好吧,我編輯了一堆發佈後的時間,並提出了很多意見,如果你在我發佈後嘗試這個權利,現在不同了,試試這個 –

+0

是的,我沒有直接嘗試它,它沒有工作。我會再試一次!謝謝 我試着刪除數據字段的新代碼,並更改了GetEvents()的參數,現在fullcalendar消失了。 – Mix

+1

如果fullcalendar消失,那麼你有一些無效的JavaScript在那裏是不會導致它的任何功能 –