简体   繁体   English

如何让AJAX将按钮点击传递给python?

[英]how do I get AJAX to pass button click to python?

I'm a novice. 我是新手。 I would like to pass a value from an HTML button click on the client's browser to a python script on the server to send a value to a PostgreSQL db. 我想从客户端浏览器上单击HTML按钮的值传递到服务器上的python脚本,以将值发送到PostgreSQL数据库。 I must be missing something. 我肯定错过了什么。 Here's what the javascript, and AJAX in the HTML look like: 这是JavaScript和HTML中的AJAX的样子:

 <div class="note" id="(Untitled)">
<script type="text/javascript" src="./jquery-2.1.1.js">
$(function () {
    $("#one").click(function () {
        $("#one").trigger("click", ["1"]);
        do_it(1);
        });
    $("#two").click(function () {
        $("#two").trigger("click", ["2"]);
        do_it(2);
        });
});

function do_it(n) {
$.ajax({
    type: "POST",
    url: "~/script_for_practice_jun20.py",
    datatype: "json",
    data: {param: 'n'}
    success: success
}),
}
)

Here's the python part that I am using... 这是我正在使用的python部分...

    def cgi_get_from_ajax(self):
        import json
        import cgi
        data = cgi.FieldStorage()
        chosen = data["param"].value
        return chosen

    def set_choice(self):
        process = Name_of_class()
        choice = process.cgi_get_from_ajax()
        entry = []
        if choice == '1':
            entry = [1,0,]
        else:
            entry = [0,1]
        return entry

My deadline is approaching. 我的截止日期临近。 Please help me. 请帮我。 Please. 请。 Please. 请。

url: "~/script_for_practice_jun20.py",

this is a server friendly url. 这是服务器友好的网址。 jquery cant use url with ~ sign. jQuery不能使用带有~符号的url。

correct you url , either relative to current page, or absolute from root directory. 更正您的url ,相对于当前页面,还是绝对来自根目录。

Did You Got Any Error In Console? 您在控制台中遇到任何错误吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM