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

2024-03-28 19:52:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我不知道如何获取发送到函数的变量。

我使用的是request.vars.variableName,我知道这个函数是有效的,只是没有接收到要正确使用的变量。如何使用web2py从python函数获取POST-sent变量?

埃塔:这是我用的密码

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)

正在查找“”而不是数据库中的任何内容。


Tags: 函数数据库jsondbdatarequesttypefunction
2条回答

这对我有效:

AJAX调用:

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

控制器:

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

希望对你有帮助

request.post_vars

如果没有请求,它们也会复制到request.vars中。获取变量

相关问题 更多 >