问题
I'm making an AJAX call to a python function. That function does a database query based on the information sent to the function.
I can't work out how to get the variable that is sent to the function.
I was using request.vars.variableName, and I know the function is valid, it's just not receiving the variable to use properly. How do I get POST sent variables from a python function, using web2py?
ETA: This is the code I was using
jQuery.ajax(
{type: "POST",
url: '../../Printed/printedballoons/cost.json', //python function
data: typeSelected,//data sent to server
dataType: 'json',
error: function(msg){$("#ajaxerror").html(msg);},
success: function(data){
balloonPrice = data.cost;
},
timeout: 2000}
);
The error was in the "data: typeSelected" line, the variable name wasnt associated with any data, so the python query:
cost=db(db.balloonprices.type==request.vars.typeSelected).select(db.balloonprices.cost)
was looking for "" as opposed to a anything that actually is in the database.
回答1:
request.post_vars
They are copied to request.vars
also if there is no request.get_vars
回答2:
This works for me:
AJAX call:
$.ajax({
type: "POST",
url: "/app/func",
data: "array="+JSON.stringify(tempArray)
}).done(function( msg ) { });
controller:
def func():
temparray = json.loads(request.post_vars.array)
Hope it'll help you
来源:https://stackoverflow.com/questions/12781187/get-data-from-post-ajax-call-using-web2py