request..args.get 什么都不存储

request..args.get stores nothing

我有以下代码。这些代码应该在 python 中获取用户的输入和处理。所有这些都应该 运行 在同一个网页中。

我已经尝试了flask.request下所有可能的功能。

在 python 文件中,

app.route("/",methods=['GET','POST'])
def homepage():
    return render_template('index.html')

@app.route("/detection",methods=['GET'])
def detections():
    code=request.args.get('code',default='',type=str)
    print(code)
    ide=Lang_Dec(code)
    lang=ide.get_lang()
    print(lang)
    return jsonify({'html':lang})

在 html 文件中,

<body>
        <div class="container">
            <div class="row">
                <div class="col-lg-8">
                        <textarea class="codemirror-textarea" id='code'></textarea>
                        <button type="button" class="btn btn-primary btn-lg" id='butt'>Guess the function!</button>
                </div>
                <div class="col-lg-4">
                    <div class="card" >
                        <h5 class="card-header">Detected Language</h5>
                        <div class="card-body">
                            <h6 class="card-title" id='lang'></h6>
                            <p class="card-text">Percentages</p>
                        </div>
                    </div>
                    <div class="card text-white bg-dark mb-3" >
                            <h5 class="card-header">IDE OUTPUT</h5>
                            <div class="card-body">
                                <p class="card-text" id='ide'></p>
                            </div>
                    </div>
                </div>
            </div>
        </div>



        <script>
            $(document).ready(function(){
                var code = $(".codemirror-textarea")[0];
                var editor = CodeMirror.fromTextArea(code, {
                    lineNumbers : true,
                    theme : "duotone-dark",
                });
            });
        </script>

        <script>
            $(document).ready(function(){
                $('#butt').click(function(){
                    var code=$('#code').val();
                    $.ajax({
                        url:"/detection",
                        type: "get",
                        data:{code:code},
                        success:function(response){
                            $('#lang').html(response.html);
                        },
                        error: function(xhr){
                            //do smtg
                        }
                    });
                });
            });
        </script>
    </body>

Python 应该从 textarea 获取用户输入,但事实证明 Python 脚本只获取 -.

这不是 Python 或 Flask 问题,您没有将文本区域包装在表单中,CodeMirror Documentation 建议如下(强调我的):

CodeMirror.fromTextArea(textArea: TextAreaElement, ?config: object)

This method provides another way to initialize an editor. It takes a textarea DOM node as first argument and an optional configuration object as second. It will replace the textarea with a CodeMirror instance, and wire up the form of that textarea (if any) to make sure the editor contents are put into the textarea when the form is submitted. The text in the textarea will provide the content for the editor.

尝试用简单的形式确认这一点:

<form action='detection'>
    <textarea id='code' name='code'></textarea>
    <button type="submit">Go!</button>
</form>

本地服务,提交带有一些随机文本输入的表单会产生以下结果:

127.0.0.1 - - [21/Jan/2019 22:50:04] "GET /detection?code=dfgdfgdfgggggggggg HTTP/1.1"

所以在你的情况下,看起来你需要在你的 Ajax 请求之前调用 cm.save(),至少这是我快速浏览文档的建议。

因此,对您的代码进行快速而粗略的修复如下所示:

<script>
    $(document).ready(function(){
        var code = document.getElementById('code');
        var editor = CodeMirror.fromTextArea(code, {
            lineNumbers : true,
            theme : "duotone-dark",
        });
       $('#butt').click(function(){
            editor.save(); // this is where the textarea content gets updated
            var c=$('#code').val();
            $.ajax({
                url:"/detection",
                type: "get",
                data:{code:c},
                success:function(response){
                    $('#lang').html(response.html);
                },
                error: function(xhr){
                    //do smtg
                }
            });
        });
    });
</script>