金字塔json视图ajax电话

时间:2014-05-14 14:44:20

标签: javascript python ajax json pyramid

我之前从未真正使用过ajax,所以请你详细说明你的答案..

我有一个Pyramid应用程序,我想通过ajax加载信息,因为预加载它是不可行的。所以我想通过金字塔视图加载我需要的信息,但我不确定如何做到这一点 我需要获取的信息是在MySQL数据库中,所以我想我需要将鼠标单击事件对象ajax_id导入到views.py中以进行查询。 (我可以毫无问题地得到ajax_id)

在我的views.py中,我有:

@view_config(route_name="info_ajax",renderer="json")
def info_ajax(self):

    #for the sake of this example, lets just return the information from the mouse click event
    A = ajax_id      #is the id of the mouse click event
    B = ajax_name    #is the name of the mouse click event

    return {
                'a' : A,
                'b' : B,
        }

我通常会预先加载所有信息,但在这种情况下需要很长时间,所以我不能在views.py中创建MySQL查询列表,然后在我的.mak中执行<script>window.coords = ${a|query_list};</script>文件。

我想在我的JavaScript代码中导入ab作为变量,以便我可以再次使用它们而无需在需要时重新加载它们。我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

已经有一个官方的金字塔教程,提供足够的背景来解决基本的AJAX任务。

您需要更多教程吗?

我最喜欢的高级教程是ToDoPyramid(Github fork)

答案 1 :(得分:1)

所以我想出了怎么做:

金字塔中的

view.py:

@view_config(route_name="info_ajax",renderer="json")
def info_ajax(self):

    #for the sake of this example, lets just return the information from the mouse click event

    A = self.request.POST.get('ajax_id')    #is the id of the mouse click event
    B = self.request.POST.get('ajax_name')   #is the name of the mouse click event

    return {
                'ID' : A,
                'Name' : B,
        }

和JS:

    $.ajax({
        type: "POST",
        url: "details",
        dataType: "json",
        data: {
            'ajax_id': iID,
            'ajax_name': sName,
        },
        success: function(data) {
                iReturned_value = data.ID;
            }, 
    })
相关问题