我以前从未真正使用过ajax,所以请您在回答中详细说明一下。
我有一个金字塔应用程序,我想通过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中列出一个views.py查询,然后在views.py文件中执行<script>window.coords = ${a|query_list};</script>
。
我希望将a
和b
作为变量导入到JavaScript代码中,这样我就可以再次使用它们,而不必在需要时重新加载它们。我该怎么做呢?
发布于 2014-05-15 13:39:56
所以我想出了怎么做:
在金字塔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,
}
在联合来文中:
$.ajax({
type: "POST",
url: "details",
dataType: "json",
data: {
'ajax_id': iID,
'ajax_name': sName,
},
success: function(data) {
iReturned_value = data.ID;
},
})
发布于 2014-05-15 08:06:04
已经有一个正式的金字塔教程提供了足够的背景来解决基本的AJAX任务。
你需要更多的教程吗?
我最喜欢的高级教程是ToDoPyramid (一个Github叉子)。
https://stackoverflow.com/questions/23657939
复制相似问题