Get data from POST ajax call using web2py

我的梦境 提交于 2019-12-21 20:58:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!