2011-11-23 40 views
1

這是我的第一個問題。我一直在搜索Stack Overflow和其他Web資源,但我無法找到FullCalendar正確接受jQuery數據屬性所需的確切答案。contentType屬性應用程序/ xml工作出錯,但應用程序/ json不是

我的意思是說,我有一個Web服務,它接受一個字符串作爲其所需的參數。我要打電話給這個* owner_email *

Web服務將採用該參數並返回由FullCalendar呈現的UserAssignments的數組。

我遇到了* owner_email *從jQuery/FullCalendar到ASP.NET Web服務的實際傳遞問題。當我將contentType更改爲'application/xml'或將其註釋掉時,它將返回一個空的XML文檔(我現在想要的),但是當我將其更改回'application/json'時,會發生錯誤json格式。任何幫助表示讚賞。另外,我想知道在FullCalendar上放置什麼活動。

這裏是我迄今所做的代碼:
的JavaScript:

$("#calendar").fullCalendar({ dayClick: function() { alert('a day was clicked!'); }, eventSources: [ 

// your event source { url: "WebService.asmx/displayUserAssignments", type: 'POST', data: { owner_email: 'test' }, processData: true, dataType: "json", contentType: 'application/json', color: 'yellow', // a non-ajax option textColor: 'black' // a non-ajax option } // any other sources... ] 

ASP.NET代碼片段:

[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public UserAssignments[] displayUserAssignments(string owner_email) { int totalAssignments = 0; int current_index = 0; UserAssignments[] compiled_user_assignments; OracleConnection openedConnection = new OracleConnection(WebConfigurationManager.ConnectionStrings["BehemothConn"].ConnectionString); openedConnection.Open(); OracleCommand query = new OracleCommand("SELECT * FROM personal_assignments_view where owner_email = :input_email", openedConnection); OracleCommand numberOfRows = new OracleCommand("select count(*) from personal_assignments_view where owner_email = :input_email", openedConnection); 

query.Parameters.Add(":input_email", OracleType.VarChar).Value = owner_email; numberOfRows.Parameters.Add(":input_email", OracleType.VarChar).Value = owner_email; 

OracleDataReader returned_result = numberOfRows.ExecuteReader(); returned_result.Read(); totalAssignments = returned_result.GetInt32(0); 

compiled_user_assignments = new UserAssignments[totalAssignments]; returned_result = query.ExecuteReader(); 

while (returned_result.Read()) { compiled_user_assignments[current_index] = new UserAssignments(); compiled_user_assignments[current_index].assignment_id = returned_result.GetInt32(0); compiled_user_assignments[current_index].owner_email = returned_result.GetString(1); compiled_user_assignments[current_index].assignment_name = returned_result.GetString(2); compiled_user_assignments[current_index].assignment_date = returned_result.GetOracleDateTime(3).Value.ToString("R"); compiled_user_assignments[current_index].assignment_description = returned_result.GetString(4); compiled_user_assignments[current_index].event_id = returned_result.GetInt32(5); compiled_user_assignments[current_index].week_day = returned_result.GetString(6); compiled_user_assignments[current_index].is_deleted = returned_result.GetString(7); compiled_user_assignments[current_index].start_time = returned_result.GetInt32(8); compiled_user_assignments[current_index].end_time = returned_result.GetInt32(9); compiled_user_assignments[current_index].is_completed = returned_result.GetString(10); compiled_user_assignments[current_index].pan_note_id = returned_result.GetInt32(11); compiled_user_assignments[current_index].pan_assign_id = returned_result.GetInt32(12); compiled_user_assignments[current_index].assignment_note = returned_result.GetString(13); current_index++; } 

openedConnection.Close(); openedConnection.Dispose(); return compiled_user_assignments; } 

UserAssignments類:

public class UserAssignments { public int assignment_id; public string owner_email; public string assignment_name; public string assignment_date; public string assignment_description; public int event_id; public string week_day; public string is_deleted; public int start_time; public int end_time; public string is_completed; public int pan_note_id; public int pan_assign_id; public string assignment_note; 

public UserAssignments() { assignment_id = -1; owner_email = ""; assignment_name = ""; assignment_date = ""; assignment_description = ""; event_id = -1; week_day = ""; is_deleted = ""; start_time = -1; end_time = -1; is_completed = ""; pan_note_id = -1; pan_assign_id = -1; assignment_note = ""; } } 

jQuery的錯誤:

{"Message":"Invalid JSON primitive: owner_email.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(Http[...]

+0

我已經決定使用xml返回,但我怎麼能夠正確地格式化輸出xml for fullcalendar解析? –

回答

0

我想你使用的是> = .NET 3.5

的問題是:你的數據不正確JSON格式。嘗試使用JSON.stringify()並生成JSON數據,如果您不需要JSON2.js,請嘗試手動以適當的JSON格式創建數據。

如果您將數組作爲JSON返回,Fullcalendar運行良好,沒有太多黑客行爲。

您將得到相同的啓動參數錯誤消息。問題在於fullcalendar試圖將開始和結束參數附加到您的數據,但開始和結束不是JSON格式。所以你必須得到fullcalendar.js並編輯下面的函數_fetchEventSource(source,callback)。

$.ajax($.extend({}, ajaxDefaults, source, { 
         data: JSON.stringify(data), 
         success: function (events) { 
          events = events.d; 
          ............................ 

而在你的web服務: -

PS:不要試圖在列表轉換內部web服務的任何format.Just返回List對象。在成功事件中,您必須從名爲'd'的'key'中獲取JSON對象,因爲在新版本中,它將對象封裝在名爲'd'的鍵中以用於ajax調用。因此,使用該密鑰訪問它。

您的Web服務的簽名(例如):

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public List<Object> functionName(argumentList) 
    { return ObjectList;} 

Webservice response captured using Firebug 如果你是在XML返回,那麼你必須在你的JavaScript和call--內適當格式創建您的活動[]對象>回調(事件)爲fullcalendar顯示。

+0

我能夠使用FullCalendar獲得XML。但是,我沒有意識到ASP.NET會自動創建單獨的元素,而不是將其序列化到一個XML字段中。 'System.Xml'和'System.Xml.Serialization'類以及'XmlAttributeAttribute'屬性都有幫助。謝謝您的幫助! 對不起格式正確。我是通過電話發佈的。 –

相關問題