我正在使用 jQuery fullcalendar 插件,我尝试过以多种不同的方式更改其他问题的答案,但没有成功,这是我的 jQuery:
$(document).ready(function () {
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
$('#calendar').fullCalendar({
theme: true,
aspectRatio: 3,
height: 1000,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
events: function (start, end, callback) {
$.ajax({
type: "POST",
url: "Default.aspx/GetEvents",
data: "{'userID':" + "'B473795D-306A-4718-804B-2813884D5B48'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (doc) {
var events = [];
var obj = $.parseJSON(doc.d);
var res = unescape(obj);
$(res).find('event').each(function () {
events.push({
title: $(this).attr('title'),
start: $(this).attr('start') // will be parsed
});
});
callback(events);
}
});
}
});
});
这是我的回复:
{"d":"[{ \"title\" : \"circleJerk\", \"start\" : \"2012-06-22\" }, { \"title\" : \"BangTheWife\", \"start\" : \"2012-06-15\" , \"end\" : \"2012-06-23\" } ]"}
最佳答案
方法的返回类型 GetEvents
是 string
不是吗?
尝试返回 List<Event>
(或者无论你的对象被调用什么),然后你就不需要经历取消转义 JSON 字符串的麻烦。
$.ajax({
type: "POST",
url: "Default.aspx/GetEvents", //return type List<Event>
data: "{'userID':" + "'B473795D-306A-4718-804B-2813884D5B48'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (doc) {
//doc.d = [Event,Event,Event] (no processing needed)
callback(events);
}
});
为了响应您的评论,返回类型为 List<> 的方法将如下所示:
[{"title": "sometitle", "start": "yourData"},{"title":"someTitle2", "start":"yourStart2"}]
翻译 ASP.NET ASMX webmethod DateTime
序列化为 JavaScript Date
对象:
DateFromASPNET = function (sNetDate) {
if (sNetDate == null) return;
var r = /\/Date\(([0-9]+)\)\//i
var matches = sNetDate.match(r);
if (matches.length == 2) {
return new Date(parseInt(matches[1]));
}
else {
return sNetDate;
}
}
关于JQuery 无法正确解析 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11166355/