金字塔json视图ajax调用

Pyramid json view ajax call

本文关键字:调用 ajax 视图 json 金字塔      更新时间:2023-09-26

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

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

In my 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作为变量,这样我就可以再次使用它们,而不必重新加载它们。我该怎么做呢?

于是我想出了办法:

in pyramid 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;
            }, 
    })

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

  • 创建金字塔的自定义用户体验-步骤09:AJAX与JSON视图

你需要更多的教程?

  • 可用的金字塔教程

我最喜欢的高级教程是ToDoPyramid(一个Github分支)

  • https://github.com/saschagottfried/todopyramid