Javascript调用Python函数

时间:2016-05-10 15:03:03

标签: javascript python ajax cgi

我知道之前已经问过这个问题,但我很困惑,很抱歉,如果已经听到了答案,但我只需要走过一段时间

我希望我的JavaScript到我的python脚本,传递两个变量,然后等待一个变量的响应。我知道需要ajax才能做到这一点。目前我有:

    $.ajax({
         type: "POST",
         url: "/static/test.py",           //location of python file
         dataType: 'json',
         data: {
             choice: value                 //number variable i want to pass
             equation: writteninput        // string variable i want to pass
         }
         }).done(function(ans) {
             finalanswer = ans             // ans is the variable i want to 
                                              get back and finalanswer is 
                                              what i want to use in the rest 
                                              of my js
         });

在我的python方面,我知道我需要使用CGI来接收数据但是我完全不知道如何做到这一点。这是我到目前为止的尝试:

    import cgitb; cgitb.enable()
    import cgi
    import os

    input_data  = cgi.FieldStorage()       #how do i get my two variables?

    .....python does stuff......

    print(ans)                             #ans is string

我已经坚持了几天这个问题,但由于我是编程新手,我一直很困惑。任何帮助都表示赞赏,并提前感谢。

0 个答案:

没有答案