0

我在打电话:

events: {
    url: '/CondominioVip/evento/evento_json.json',
    error: function() {
        alert('there was an error while fetching events!');
    }
}

我也尝试添加type: 'POST ,但它也没有工作。

我的控制器:

def evento_json():
    events= "[{'title':'event1','start':'2010-01-01'},{'title':'event3','start':'2010-01-09 12:30:00','allDay':False}]"
    return events

来自浏览器的测试调用 ( http://localhost:8000/CondominioVip/evento/evento_json.json):

[{'title':'event1','start':'2010-01-01'},{'title':'event3','start':'2010-01-09 12:30:00',' allDay':假}]

来自 web2py ajax 函数的请求:

接受:应用程序/json,文本/javascript,/;q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:pt-BR,pt;q=0.8,en -US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:31
Content-Type:application/x-www-form-urlencoded
Cookie:session_id_admin=127.0。 0.1-f9db7d99-4e7e-4bae-a229-d6614e9599f0;cvip_language=pt-BR;session_id_condominiovip=127.0.0.1-b820cbe3-9a55-40bb-8618-8d3f9a43f7c2
主机:localhost:8000
来源:http://localhost:8000
Referer:http://localhost:8000/CondominioVip/evento/index/2
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5
X-Requested-With:XMLHttpRequest

响应标头:

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Length:105
Content-Type:application/json
Date:Tue, May 29 2012 02:54:04 GMT
到期时间:2012 年 5 月 29 日星期二 02:54:03 GMT
Pragma:no-cache
Server:Rocket 1.2.4 Python/2.7.3
Set-Cookie:cvip_language=pt-BR; 到期=格林威治标准时间 2012 年 5 月 30 日星期三 02:54:04;路径=/,session_id_condominiovip=127.0.0.1-b820cbe3-9a55-40bb-8618-8d3f9a43f7c2;路径=/
X-Powered-By:web2py

回复:

[{'title':'event1','start':'2010-01-01'},{'title':'event3','start':'2010-01-09 12:30:00',' allDay':假}]

我别无选择。任何帮助将不胜感激。

更新:

我在 Chrome 上安装了 JsonView 扩展,因此返回字符串不被视为 json 响应。

我做了一些改变:

def evento_json():
    rows = db(evento.id>0).select(evento.id,evento.titulo,evento.data_hora_inicio,evento.data_hora_fim)
    events = []
    for row in rows:
        event = {'title': row['titulo'],
                 'start': row['data_hora_inicio'],
                 'end': row['data_hora_fim'],
                 'allDay': False,
                 'url': URL(c='evento', f='index', args=[row['id']], extension=False)}
        events.append(event)
    return events

但是 web2py 会抛出一个错误。在将其发送到 generic.json 之前,我已经打印了“events”和“json(events)”,格式正是 fullcalendar 所期望的。

我发现停止错误的方法是:

在控制器中:

def evento_json():
    import datetime
    start = datetime.datetime.fromtimestamp(int(request.vars['start'])).strftime('%Y-%m-%d %H:%M:%S')
    end = datetime.datetime.fromtimestamp(int(request.vars['end'])).strftime('%Y-%m-%d %H:%M:%S')
    set = db((evento.id>0) &
             (evento.data_hora_inicio >= start) &
             (evento.data_hora_fim <= end))

    if (not auth.has_membership(auth.id_group(role='site_admin'), auth.user.id)) and \
       (not auth.has_membership(auth.id_group(role='cond_admin'), auth.user.id)):
        set = set(evento.flag_disp==True)

    rows = set.select(evento.id,
                      evento.titulo,
                      evento.data_hora_inicio,
                      evento.data_hora_fim,
                      evento.flag_disp)

    events = []
    for row in rows:
        event = {'title': row['titulo'],
                 'start': row['data_hora_inicio'],
                 'end': row['data_hora_fim'],
                 'allDay': False,
                 'url': URL(c='evento', f='index', args=[row['id']], extension=False),
                 'color': 'blue' if row['flag_disp'] is True else 'red'}
        events.append(event)

    if events:
        from gluon.serializers import json
        return XML(json(events))
    else:
        return '{}'

并使用以下内容创建视图 evento/evento_json.json:

{{=response._vars)}}

有用!但对我来说似乎是一个错误。我不确定我是否做错了什么。

4

1 回答 1

0

也许尝试:

events= "[{'title':'event1','start':'2010-01-01'},{'title':'event3','start':'2010-01-09T12:30:00Z','allDay':false}]"

(根据API将 event3 开始日期/时间更改为 ISO8601 格式,并将“False”更改为“false”。)

于 2012-05-29T04:49:50.043 回答