This question comes close to solving my problem, but I'm not using JQuery Ajax and do not want to post data from a form. I’m trying to post data (a variable) from the script itself.
Before passing the JS variable, I am just simply trying to pass a test string to the Python Bottle route, modify it and return it back:
HTML:
function GreetingText()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var output = xmlhttp.responseText;
document.getElementById("apparel_text").innerHTML = output;
}
}
xmlhttp.open("POST", "/getinfo", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("fname=Bob");
Event:
onclick=“GreetingText()”
Bottle:
@route('/getinfo', method="POST")
def ajax_example():
fname = requests.post(‘fname’)
text = "Greetings " + fname
return(text)
The issue is at either .send(fname=“Bob”) on the client-side or how I am receiving fname on the server-side. I’ve tried every request combination possible with no success
My working code, before I tried to pass "fname" was:
HTML:
function GreetingText()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var output = xmlhttp.responseText;
document.getElementById("apparel_text").innerHTML = output;
}
}
xmlhttp.open("GET", "/getinfo", true);
xmlhttp.send();
Bottle:
@route('/getinfo')
def ajax_example():
text = "Greetings"
return (text)
This returned "Greetings" to the element that I needed.
question from:
https://stackoverflow.com/questions/65924233/sending-a-javascript-variable-value-to-a-python-bottle-route-with-ajax 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…