Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

Sending a JavaScript Variable Value to a Python Bottle Route with Ajax

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...