使用web2py从POST ajax调用获取数据

时间:2012-10-08 11:52:37

标签: python ajax web2py

我正在对python函数进行AJAX调用。该函数根据发送给函数的信息执行数据库查询。

我无法弄清楚如何获取发送给函数的变量。

我正在使用request.vars.variableName,我知道该函数是有效的,它只是没有接收变量才能正常使用。如何使用web2py?

从python函数获取POST发送变量

ETA:这是我正在使用的代码

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

错误发生在“data:typeSelected”行中,变量名称与任何数据都没有关联,所以python查询:

cost=db(db.balloonprices.type==request.vars.typeSelected).select(db.balloonprices.cost)

正在寻找“”,而不是实际存在于数据库中的任何东西。

2 个答案:

答案 0 :(得分:5)

这对我有用:

AJAX电话:

       $.ajax({
          type: "POST",
          url: "/app/func",
          data: "array="+JSON.stringify(tempArray)
       }).done(function( msg ) { });

控制器:

def func():    
   temparray = json.loads(request.post_vars.array)

希望它能帮到你

答案 1 :(得分:2)

request.post_vars

如果没有request.get_vars

,它们也会被复制到request.vars
相关问题