我正在尝试采用用户突出显示的内容,以便对其进行处理以进行数据库查询。虽然我似乎能够成功地获得突出显示的内容,但似乎并没有将这些信息传递到实际的数据库查询中。没有任何错误消息。我希望某个地方有错别字,而且很容易解决,在任何情况下,都应该感谢经验丰富的人的额外帮助。我正在使用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 %}
默认情况下,POST将发送
Content-Type: application/x-www-form-urlencoded
这需要发送一个URL编码的表单。看来您正在发送纯文本,这需要添加
xhttp.setRequestHeader('Content-Type', 'text/plain');
.send()
之前