从python文件中的ajax post请求获取发布数据

Pet*_*ter 5 python ajax jquery

我正在尝试使用ajax post请求发布一些数据并执行python文件,检索python文件中的数据,然后返回结果.

我有以下ajax代码

        $(function () {
                    $("#upload").on("click", function (e) {
                        $.ajax({
                            type: 'post',
                            url: "test1.py",
                            data: {'param1':'abc'},
                            async: false,
                            success: function (response) {
                                console.log(response);
                            }
                        }).done(function (data) {
                            console.log(data);
                        });
                    });
                });
Run Code Online (Sandbox Code Playgroud)

以及以下python文件

    #! /usr/bin/python
    from __future__ import print_function
    import cgi, cgitb
    def index():
        data = cgi.FieldStorage()
        mydata = data['param1'].value
        return return "test"
Run Code Online (Sandbox Code Playgroud)

我在param1上得到了一个关键词 - >"KeyError:'param1'".我也试过使用getValue(data ['param1'].看起来像将ajax调用中的数据传输到我想要执行的python文件的问题......但我无法弄清楚为什么.

提前致谢

Pet*_*ter 5

我设法找到了解决方案.我希望其他人也可以使用它.

通过以下python代码我已经magedd从ajax调用获取postdata.

    def index(req):
        postData = req.form
        json = str(postData['param'].value)
Run Code Online (Sandbox Code Playgroud)