JQuery不能正确解析JSON响应 [英] JQuery isn't parsing JSON response properly
问题描述
我使用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,
高度:1000,
标题:{
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' },
的内容类型: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({
标题:$(this).attr('title'),
start:$(this).attr('start')//将被解析
});
}) ;
callback(events);
}
});
}
});
});
$ c $
{/ / p $ p
这是我的回应: d:[{\title \:\circleJerk \,\start \:\2012-06-22\},{\title \\ :BangTheWife,开始,\\2012-06-15 \,\ end \:\2012-06-23 \}]}
解决方案 您的方法返回类型 GetEvents
是 string
是不是?
试着返回一个 List< Event>
(或者你的对象被调用),然后你不需要
$。ajax({
类型:POST,
url:Default.aspx / GetEvents,//返回类型List< Event>
data:{'userID':+'B473795D-306A-4718-804B-2813884D5B48'},
contentType:application / json; charset = utf-8,
数据类型:json,
成功:函数(doc){
//doc.d = [事件,事件,事件](不需要处理)
回调(事件);
}
});
作为对您的评论的回应,返回类型为List<>的方法如下所示:
[{title:sometitle,start:yourData},{title:someTitle2, start:yourStart2}]
翻译ASP.NET ASMX webmethod DateTime
序列化为JavaScript Date
object:
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;
}
}
I am using jQuery fullcalendar plugin , I have tried altering this many different ways from answers to other questions with no luck, here is my 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);
}
});
}
});
});
Here is my response :
{"d":"[{ \"title\" : \"circleJerk\", \"start\" : \"2012-06-22\" }, { \"title\" : \"BangTheWife\", \"start\" : \"2012-06-15\" , \"end\" : \"2012-06-23\" } ]"}
解决方案 Your return type from your method GetEvents
is string
isn't it?
Try returning a List<Event>
(or whatever your object is called) and then you don't need to go through the mess of unescaping a JSON string.
$.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);
}
});
In response to your comment, a method with return type of List<> will look like :
[{"title": "sometitle", "start": "yourData"},{"title":"someTitle2", "start":"yourStart2"}]
Translating ASP.NET ASMX webmethod DateTime
serialization to JavaScript Date
object:
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响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文