【发布时间】:2023-03-07 21:19:01
【问题描述】:
我正在尝试获取用户突出显示的内容,以便处理它以查询数据库。虽然我似乎能够成功地获得突出显示的内容,但似乎没有将这些信息传递给实际的数据库查询。没有任何错误消息。我希望某处有一个错字,而且很容易解决,在任何情况下,如果有经验丰富的人多加一双眼睛,我将不胜感激。我正在使用 Python Flask 开发一个供个人使用的网络应用程序,这里是代码
{% block content %}
<head>
<style>
div.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
float: right;
width: 30%;
}
</style>
</head>
<h2 style="text-align: center;">{{ name }}</h2>
<div class="sticky" id="sticky_display">
<button type="button" onclick ="getSelectedText()" style="background: darkblue; color: white;">Query Database</button>
<form name="testform">
<textarea id="selectedtext">
</textarea>
</form>
</div>
<div class="row">
<div class="col-xs-8 col-sm-8 col-md-8 col-lg-8" style="white-space: pre-line;" id="text_display">
{{ text }}
</div>
</div>
<script>
function getSelectedText() {
var selectedText = '';
if (window.getSelection) {
selectedText = window.getSelection();
}
// document.getSelection
else if (document.getSelection) {
selectedText = document.getSelection();
}
// document.selection
else if (document.selection) {
selectedText = document.selection.createRange().text;
} else return;
function(selectedText) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
document.getElementById('selectedtext').value = this.responseText;
}
xhttp.open("POST", "/query_database', true);
xhttp.send(selectedText);
}
}
</script>
{% endblock %}
【问题讨论】:
-
不是说这是问题所在,而是您在
/query_database周围的引号不匹配。 -
你说得对。
-
有时CORSing你的应用可以解决这个问题:flask-cors.readthedocs.io/en/latest
-
感谢您的意见,我最终只是改变了我想做的事情并且成功了。不理想,但你能做什么。
标签: python ajax flask xmlhttprequest