我以前从未真正使用过 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 文件中。
我要导入a
和 b
在我的 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/