I have never really worked with ajax before, so would you please be elaborate in your answers..
I have a Pyramid application where I want to load information via ajax because pre-loading it would not be feasible. So I want to load the information I need through a pyramid view, but I am unsure how to do this.
The information I need to fetch is in a MySQL database, so I guess I need to import the mouse click event object ajax_id
into views.py in order to do the query. (I can get ajax_id without a problem)
In my views.py I have:
@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,
}
What I would usually do is to preload all information, but that would take to long in this case, so I cannot just make a list of MySQL queries in views.py and then do <script>window.coords = ${a|query_list};</script>
in my .mak file.
I want to import a
and b
in my JavaScript code as variables, so that I can use them again without having to reload them if need be. How do I go about doing this?
So I figured out how to do it:
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,
}
and in JS:
$.ajax({
type: "POST",
url: "details",
dataType: "json",
data: {
'ajax_id': iID,
'ajax_name': sName,
},
success: function(data) {
iReturned_value = data.ID;
},
})