簡體   English   中英

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文檔建議以下內容(強調我的意思):

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

此方法提供了另一種初始化編輯器的方法。 它以textarea DOM節點作為第一個參數,並以可選的配置對象作為第二個參數。 它將用CodeMirror實例替換textarea,並連接該textarea的表單(如果有),以確保 提交 表單時將編輯器內容放入textarea中 文本區域中的文本將為編輯器提供內容。

嘗試通過簡單的形式進行確認:

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

在本地服務,提交帶有一些隨機文本輸入的表單將產生以下結果:

127.0.0.1--[2019年1月21日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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM