通过ajax获取返回值

时间:2013-09-22 19:41:02

标签: javascript python ajax

我有一个python脚本,我在一个表单中使用一个按钮和一些带有ajax的javascript调用。我真的很难找到python脚本返回的返回变量。

所有返回的信息似乎都发生在错误的顺序中,这可能不是这种情况,而是它对我的看法。我想要的是能够获取我的python脚本返回的数据。怎么这么简单就这么难!!

任何帮助将不胜感激。感谢

这是我的代码:

            <script>
            function drawgraph() {
                    var x = draw()
                    alert(x)
            };
            </script>
            <script>
            function draw() {
                    alert("Hello");
                    $.ajax({
                            url: "/currentcost.py",
                            success: function(response){
                            alert("Success");
                            return response;
                            //here you do whatever you want with the response variable
                            }
                    });
                    alert("World");
            }
            </script>

我得到的回答是“你好”,“世界”,“未定义”和“成功”

这告诉我ajax函数实际上并没有完成,直到我的javascript函数完成后才能帮我实际掌握返回的变量,所以我可以在以后的javascript中使用它

克里斯

EDIT。

    <head>
            <title>Line Chart</title>
            <script>
            var ajaxResponse = ""
            </script>
            <script src="Chart.js"></script>
            <script src="jquery.js"></script>
            <script>
                    function drawgraph() {
                            function handleResponse(response) {
                                    ajaxResponse = response;
                            }
                            draw(handleResponse);
                    };

                    function draw(handleResponse) {
                    $.ajax({
                    url: "/currentcost.py",
                    success: function(response){
                            handleResponse(response);
                    }
                    });
            }
            </script>
            <script>
                    function alertbox() {
                            alert(ajaxResponse);
                    }
            </script>

Currentcost.py:

导入MySQLdb

def index(req):     dtbox = req.form.getfirst('dt','')     tmbox = req.form.getfirst('tm','')

con = MySQLdb.connect('localhost', 'root', '', 'mydb')

with con:
    cur = con.cursor(MySQLdb.cursors.DictCursor)
    s = "SELECT tmp, watts FROM currentcost WHERE dt ='" + dtbox + "' and tm like '" + tmbox + "%'"
    cur.execute (s)
    rows = cur.fetchall()

    x=""
    y=""
    for row in rows:
        x=x+row["watts"]+","
        y=y+row["tmp"]+","

x="data:["+x+"]"
y="data:["+y+"]"

con.close()

req.write(x)

3 个答案:

答案 0 :(得分:1)

这就是异步编程的本质。它是非阻塞的。您必须在回调中启动需要response的代码的执行。

答案 1 :(得分:0)

Ajax中的“A”是异步的。这意味着HTTP请求已完成,其余JS代码也会执行。当请求完成时,您无法依赖,如果 将完成。依赖于请求返回的值的任何工作都必须在回调中完成(success

function (response) {
    alert("Success");
    // this will alert the response
    alert(response);
}

此外,来自回调的return不会去任何地方。您也不会从draw返回。

你可以使用deferred来使语法更加可口:

function drawgraph() {
    var x = draw();
    x.done(function (response) {
        alert(x);
    });
}
function draw() {
    return $.ajax(/* your code */

答案 2 :(得分:0)

Javascript是异步的。当您提醒x时,它很可能是undefined,因为尚未收到ajax响应,或者甚至没有启动ajax。传递一个可以处理响应的回调函数,而不是返回它:

//global variable holding the response
//substitutes your x
var ajaxResponse;

function drawgraph() {
    function handleResponse(response) {
        ajaxResponse = response; 
        //here you may want to do something with the response
        //like calling a function that uses the global variable ajaxResponse

        //alert(response);
    }
    draw(handleResponse);
};

function draw(handleReponse) {
    $.ajax({
       url: "/currentcost.py",
       success: function(response){
           handleResponse(response);
       }
    });
}

修改

在我的小测试中,我只是在一个

中调用了drawgraph
$(document).ready(function() {
  drawgraph();
}

例如,在页面加载时。一旦。很明显你想如何触发它。如果点击按钮,按钮id为“按钮”,<button id="button">click</button>

$('#button').click(function() {
  drawgraph();
}

执行链:

  1. 东西叫做drawgraph
  2. drawgraph调用draw并传递回调函数
  3. 关于ajax成功,使用响应
  4. 调用回调函数

    因为javascript是异步的,所以你必须“等待”才能“继续使用剩下的代码”,最好的方法是从$ .ajax内部继续执行成功。