解析数据时,json在位置0处出现意外令牌u
很明显,我是这个问题的众多拥护者之一,过去一个小时左右我一直在谷歌上搜索,但似乎无法解决我的问题.
it is clear that I am one of many with this issue, I've been googling for the past hour or so but I cannot seem to fix my issue.
我正在尝试使用AJAX从我的控制器中获取数据以放入jQuery FullCalendar中, 现在,我对我的控制器非常有信心,但仍然收到错误消息:
I am trying to fetch data from my controller using AJAX to put into the jQuery FullCalendar, now that I am fairly confident about my controller, I still get an error saying:
json在位置0处出现意外令牌U
unexpected token U in json at position 0
所以一定有什么问题.
这是我用来从Controller中的数据库中获取数据的脚本:
here's the script I use to get data from my database in my Controller:
[HttpPost, ActionName("List")]
[WebMethod(EnableSession = true)]
public ActionResult List()
{
temphrmEntities db = new temphrmEntities();
List<medewerker_melding> eventList = db.medewerker_melding.ToList();
// Select events and return datetime as sortable XML Schema style.
var events = from ev in eventList
select new
{
id = ev.ID,
title = ev.medewerker.voorvoegsel + ". " + ev.medewerker.achternaam,
desc = ev.Omschrijving,
start = ev.datum_van.ToString(),
end = ev.datum_tot.ToString(),
user = ev.medewerkerID,
melding = ev.meldingID
};
// Serialize to JSON string.
JavaScriptSerializer jss = new JavaScriptSerializer();
String json = jss.Serialize(events);
Debug.WriteLine("Json:"+json);
return Json(json, JsonRequestBehavior.AllowGet);
}
这是它返回的Json格式:
This is the Json format that it returns:
[
{
"id":1,
"title":"K. Keesen",
"desc":"zelf ziek gemeld",
"start":"2-2-2018 13:00:00",
"end":"5-2-2018 13:00:00",
"user":15,
"melding":1
},
{
"id":3,
"title":"K. Keesen",
"desc":null,
"start":"2-2-2018 13:00:00",
"end":"5-2-2019 13:00:00",
"user":15,
"melding":1
},
{
"id":5,
"title":"K. Keesen",
"desc":null,
"start":"14-2-2018 08:30:00",
"end":"",
"user":15,
"melding":1
},
{
"id":6,
"title":"K. Keesen",
"desc":"srgsrgrgdrgdrgd",
"start":"7-2-2018 08:30:00",
"end":"",
"user":38,
"melding":13
},
{
"id":7,
"title":"T. test",
"desc":null,
"start":"14-2-2018 08:30:00",
"end":"21-2-2018 17:00:00",
"user":63,
"melding":10
},
{
"id":8,
"title":"K. Keesen",
"desc":null,
"start":"16-2-2018 08:30:00",
"end":"23-2-2018 17:00:00",
"user":28,
"melding":14
},
{
"id":9,
"title":"K. Keesen",
"desc":null,
"start":"14-2-2018 08:30:00",
"end":"",
"user":33,
"melding":12
},
{
"id":10,
"title":"K. Keesen",
"desc":"fvghbj",
"start":"22-2-2018 08:30:00",
"end":"",
"user":15,
"melding":11
},
{
"id":11,
"title":"K. Keesen",
"desc":null,
"start":"15-2-2018 08:30:00",
"end":"22-2-2018 17:00:00",
"user":15,
"melding":1
},
{
"id":12,
"title":"K. Keesen",
"desc":null,
"start":"23-2-2018 08:30:00",
"end":"",
"user":15,
"melding":1
},
{
"id":13,
"title":"K. Keesen",
"desc":"Test take #25",
"start":"7-2-2018 08:30:00",
"end":"23-2-2018 17:00:00",
"user":15,
"melding":1
},
{
"id":14,
"title":"K. Keesen",
"desc":null,
"start":"8-2-2018 08:30:00",
"end":"",
"user":15,
"melding":1
}
]
最后是我使用ajax的功能:(我不太擅长使用ajax,所以请复制粘贴它)
and finally my function with ajax: (I'm not very good at ajax so copy pasted it)
function GenerateCalendar() {
$("#calendar").fullCalendar({
theme: true,
header: {
left: "prev,next today",
center: "title",
right: "month,agendaWeek,agendaDay"
},
defaultView: 'month',
selectable: true,
selectHelper: true,
editable: true,
eventLimit: true,
events: function (start, end, callback) {
$.ajax({
type: "POST", //WebMethods will not allow GET
url: '@Url.Action("List/medewerker_melding")', //url of a webmethod - example below
//data: "", //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')
});
});
callback(events);
}
});
},
select: function (start, end) {
var start = moment(start).format();
var end = moment(end).format();
$('#startDate').val(start);
$('#endDate').val(end);
$('#eventModal').modal();
if ($('#eventModal')) {
$(".meldingForm").submit(function () {
//insertEvents();
});
$('#calendar').fullCalendar('renderEvent', events, true);
}
$('#calendar').fullCalendar('unselect');
},
});
}
谢谢!
您不需要此行:
var obj = $.parseJSON(doc.d);
当您告诉jQuery期望的数据类型是json(dataType: "json"
)时,它将尝试自动为您解析它,或者如果它不是有效的JSON对象,则会失败(请参见
When you tell jQuery that the data type to expect is json (dataType: "json"
) it'll try to parse it automatically for you or fail if it's not a valid JSON object (see jQuery.getJSON() for more details).
尝试以下方法:
var obj = doc.d;
更新:
d
是未定义的.尝试将代码更改为此:
d
is, indeed, undefined. Try changing your code to this:
var events = []; //javascript event object created here
$.each( doc, function( index, obj ) {
events.push({
title: obj.title,
start: obj.start,
end: obj.end
});
});
callback(events);