[英]request..args.get stores nothing
I have the following code. 我有以下代码。 These code supposed to get user's input and processed in python.
这些代码应该获取用户的输入并在python中进行处理。 All these are supposed to run in the same webpage.
所有这些都应该在同一网页上运行。
I have tried all possible function under flask.request
. 我已经在
flask.request
下尝试了所有可能的功能。
Inside the python file, 在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})
Inside the html file, 在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>
The Python is supposed to get the user input from the textarea
, but it turns out that the Python script only getting a -
. Python应该从
textarea
获取用户输入,但是事实证明Python脚本仅获得-
。
This isn't a Python or Flask issue, you have not wrapped your textarea inside a form, and the CodeMirror Documentation suggests the following (emphasis mine): 这不是Python或Flask问题,您还没有将文本区域包装在表单中,并且CodeMirror文档建议以下内容(强调我的意思):
CodeMirror.fromTextArea(textArea: TextAreaElement, ?config: object)
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.
它以textarea DOM节点作为第一个参数,并以可选的配置对象作为第二个参数。 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 .
它将用CodeMirror实例替换textarea,并连接该textarea的表单(如果有),以确保 在 提交 表单时将编辑器内容放入textarea中 。 The text in the textarea will provide the content for the editor.
文本区域中的文本将为编辑器提供内容。
Try and confirm this with a simple form: 尝试通过简单的形式进行确认:
<form action='detection'>
<textarea id='code' name='code'></textarea>
<button type="submit">Go!</button>
</form>
Serving locally, submitting the form with some random text input yields the following: 在本地服务,提交带有一些随机文本输入的表单将产生以下结果:
127.0.0.1 - - [21/Jan/2019 22:50:04] "GET /detection?code=dfgdfgdfgggggggggg HTTP/1.1"
127.0.0.1--[2019年1月21日22:50:04]“ GET / detection?code = dfgdfgdfgggggggggg HTTP / 1.1”
So in your case, it looks like you need to call cm.save()
prior to your Ajax request, at least that is what a quick glance at the documentation suggests to me. 因此,在您的情况下,您似乎需要在Ajax请求之前调用
cm.save()
,至少这是快速浏览文档对我的建议。
So a quick and dirty fix to your code would look like this: 因此,对您的代码进行快速而肮脏的修复将如下所示:
<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.