2013-09-22 87 views
0
//My web-service method WebService1.asmx 

[WebMethod]  
[ScriptMethod]  
public string GetAllEvents()  
{  
    var list = new List<string>(); 
    list.Add("[{\"id\":\"36\"title\":\"Birthday party\",\"start\":\"2013-09-18\",\"end\":\"2013-09-18\",\"allDay\":false}]"); 
    JavaScriptSerializer jss = new JavaScriptSerializer(); 
    string strJSON = jss.Serialize(list.ToArray()); 
    return strJSON; 
} 

//My jQuery snippet 

$("#fullcalendar").fullCalendar({ 

eventSources: 

[  
    {   
    url: "http://localhost:49322/WebService1.asmx/GetAllEvents",   
    type: 'POST',   
    dataType: "json",   
    contentType: "application/json; charset=utf-8",   
}  
]  
}); 
+0

請提供更多的信息(例如你的HTML丟失)或 - 甚至更好 - 把一個jsFiddle放在一起。 – chopper

+0

對於它的價值,我最近發佈了一些關於如何使用ASHX文件執行此操作的C#代碼:http://mikesmithdev.com/blog/fullcalendar-json-feed-httphandler-csharp/如果這是一個選項而不是web服務,讓我知道,我會張貼代碼作爲答案。 – MikeSmithDev

回答

2

你在錯誤的軌道上。

  • 不要使用字符串操作手動形成你的json字符串(就你而言,它是一個無效的json)。
  • 不要從Web方法返回的字符串,返回真正的對象

應該是這樣的:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public List<Event> GetAllEvents() 
{ 
    List<Event> events = ....... 
    ..fill the list.... 
    return evetns; 
} 



public class Event 
{ 
    public int id { get; set; } 
    public string title { get; set; } 
    public string start { get; set; } 
    public string end { get; set; } 
    public bool allDay { get; set; } 
} 
+0

public class Events { public string start; 公共字符串標題; } 公共列表 GetAllEvents() { 列表事件=新列表(); events.Add(new Events(){title =「Birthday」,start =「2013-09-23」}); 返回事件; } //在此之後如何將其轉換爲JSON對象並在哪裏? –

+0

@ChaitanyaMoganti不需要手動將對象序列化爲json。它會自動完成,請參閱屬性('[ScriptMethod(ResponseFormat = ResponseFormat.Json)]') – I4V

+0

$( 「#fullcalendar」)fullCalendar({ eventSources: [ { 網址:「HTTP: //localhost:49322/WebService1.asmx/GetAllEvents」, 類型: 'POST', 數據類型: 「JSON」, 的contentType: 「應用程序/ JSON;字符集= UTF-8」, } ] }); 它不工作如果我喜歡這樣做.. –