FullCalendar and WCF
I've been trying to get data from a wcf service and into the fullcalendar control. However, ive had no luck and wondered what i was doing wrong.
jscript :
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
开发者_StackOverflow社区 right: 'month,basicWeek,basicDay'
},
editable: false,
height: 200,
aspectRatio: 100,
events: "http://localhost:63295/_services/Service2.svc/DoWork/"
etc...
WCF interface :
[ServiceContract]
public interface IService2
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
string[] DoWork();
}
WCF Service :
public string[] DoWork()
{
// Add your operation implementation here
SortedDictionary<string, string> d = new SortedDictionary<string, string>();
NameValueCollection AE = new NameValueCollection();
SqlDataReader sdr = ReadData("SelectALLAE");
while (sdr.Read())
{
AE.Add("title", sdr["AE_EmployeeID"].ToString() + " " + sdr["AE_EmployeeName"].ToString() + " " + sdr["AE_EventCode"].ToString());
AE.Add("start", sdr["AE_StartDateTime"].ToString());
AE.Add("end", sdr["AE_EndDateTime"].ToString());
}
return AE.GetValues(0).ToArray();
}
Web.config :
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="CountryProvinceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="CountryProvinceBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="CountryProvinceBehavior" name="TimesheetsV2._0_Investigations._services.Service2">
<endpoint address="" binding="webHttpBinding" contract="TimesheetsV2._0_Investigations._services.IService2" behaviorConfiguration="CountryProvinceBehavior"/>
</service>
</services>
</system.serviceModel>
I've successfully connected to this wcf on a page without the full calendar. This was so i could test how to connect to the wcf service via jquery.
But when i use the fullcalendar event option, nothing happens. it doesn't even connect the wcf service at all ( i tried to do a debug on the service and nothing happened ).
any help would be appreciated
thanks
I know this is 7 months old but - try taking the trailing slash off the url call in the $.ajax call.
I think you want : "http://localhost:63295/_services/Service2.svc/DoWork"
try this:
$("#calendar").fullCalendar({
events: {
url: 'http://localhost:63295/_services/Service2.svc/DoWork',
type: 'POST',
data: '{}',
error: function () {
alert('there was an error while fetching events!');
},
color: 'yellow', // a non-ajax option
textColor: 'black' // a non-ajax option
}
});
Also - consider changing the bodystyle of your webinvoke call BodyStyle parameter to BodyStyle = WebMessageBodyStyle.WrappedRequest (I've read you'll get an error with plain wrapped). And not sure if you're doing it already, but be sure to code your start and end values in your list / array as unix time stamps or one of the allowed date types. your web.config is good.
Here's something I've tested (.net 4.0) with the above fullcalendar ajax call except I used a list not an array - I'm presuming you may be reading in standard dates which won't work either, so there's a function here to convert the dates to unix timestamps on the fly, just dodge it if you're already working with unix time stamps.
[WebInvoke(Method="POST", BodyStyle=WebMessageBodyStyle.WrappedRequest, ResponseFormat=WebMessageFormat.Json)]
[OperationContract]
public List<AELIST> DoWork()
{
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["YourConnNameInWebConfig"].ConnectionString;
sqlconn.Open();
string sqlstring = "Select * from YourTable";
SqlCommand sqlcomm = new SqlCommand(sqlstring, sqlconn);
SqlDataReader sreader = sqlcomm.ExecuteReader();
List<AELIST> AE = new List<AELIST>();
while (sdr.Read())
{
DateTime dsx = Convert.ToDateTime(sdr["AE_StartTime"]);
Double mystart = ConvertToTimestamp(dsx);
DateTime dex = Convert.ToDateTime(sdr["AE_EndDateTime"]);
Double myend = ConvertToTimestamp(dex);
AELIST AEEntry = new AELIST<>();
AEEntry.title = sdr["AE_EmployeeID"].ToString() + " " + sdr["AE_EmployeeName"].ToString() + " " + sdr["AE_EventCode"].ToString();
AEEntry.start = mystart.ToString();
AEEntry.end = myend.ToString();
AE.Add(AEEntry);
}
sqlconn.Close();
sdr.Close();
sreader.Dispose();
sqlcomm.Dispose();
return AE;
}
private double ConvertToTimestamp(DateTime value)
{
TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
return (double)span.TotalSeconds;
}
public class AELIST
{
public string title {get; set;}
public string start {get; set;}
public string end {get; set;}
}
精彩评论