【问题标题】:Get data from POST ajax call using web2py使用 web2py 从 POST ajax 调用中获取数据
【发布时间】:2012-10-08 11:52:36
【问题描述】:

我正在对 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)

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

【问题讨论】:

  • 解决办法是要有"data: {dataSelected: value},"

标签: python ajax web2py


【解决方案1】:

这对我有用:

AJAX 调用:

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

控制器:

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

希望对你有帮助

【讨论】:

    【解决方案2】:
    request.post_vars
    

    如果没有 request.get_vars,它们也会被复制到 request.vars

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多