【问题标题】:CGI python doesn't print when call getvalue调用getvalue时CGI python不打印
【发布时间】:2014-05-28 13:56:53
【问题描述】:

我用 ajax jquery 发送一个 json,我想用 python 在一个 txt 文件中打印数据: 客户:

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
        $(function(){
            $('#clickme').click(function(){
                alert('Im going to start processing');
                $.ajax({
                    url: "test.py",
                    type: "post",
                    datatype:"json",
                    data: {'key':'value','key2':'value2'},
                    success: function(response){
                        alert(response.message);
                        alert(response.keys);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <button id="clickme"> click me </button>
</body>

服务器:

import json`import cgi`
fs = cgi.FieldStorage()
out_file = open("test.txt","w")
for k in fs.keys(): 
    out_file.write(fs.getvalue(k))

但代码不打印任何内容

【问题讨论】:

  • 日志中的错误是什么?一方面,您不使用 sys 导入。另外你不import cgi
  • 很抱歉,我忘记粘贴导入 cgi。错误是代码不打印任何内容

标签: jquery python ajax json


【解决方案1】:

为帮助调试问题,请将整个环境写入文件。这将是所有 GET 参数。

  1. 在您的 Ajax 代码中将 'post' 更改为 'get'

  2. 试试这样的代码(未经测试):

    导入操作系统 使用 open('/tmp/output.log','a') 作为输出: outf.write(repr(os.environ)) outf.write('\n')

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 2014-12-19
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 2021-12-10
    相关资源
    最近更新 更多