javascript - Pyramid json View ajax调用

标签 javascript python ajax json pyramid

我以前从未真正使用过 ajax,所以请您在回答中详细说明..

我有一个 Pyramid 应用程序,我想通过 ajax 加载信息,因为预加载它是不可行的。所以我想通过 Pyramid View 加载我需要的信息,但我不确定该怎么做。
我需要获取的信息在 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 查询然后执行 <script>window.coords = ${a|query_list};</script>。在我的 .mak 文件中。

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

最佳答案

所以我想出了办法:

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

关于javascript - Pyramid json View ajax调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23657939/

相关文章:

javascript - 在 iFrame 中打开 CSS 文件时阻止浏览器提示文件下载?

javascript - 有没有办法在 android webview 应用程序中使用表情符号?

javascript - Acrobat Javascript 正则表达式

python - Flask 重定向时返回 JWT token

python - 在openpyxl中查看行值

javascript - 如何更改一页 slider 网站的网址

javascript - 当 Loader 出现时淡出我的页面

javascript - Lodash remove 根据 id 属性从数组中删除一个对象

python - 将 Python 解析为 Golang - DateTime

javascript - "/"是否等于 "window.location.protocol + '//' + window.location.host"