我想在python变量中发送这个变量值,以执行不同的任务。
var d="string"
我不想通过URL发送变量值。我想使用一些像这样的代码。
@app.route("/", methods=['POST'])
def hello1():
d = request.form['n'] #here i take value from name of html element
发布于 2019-03-01 08:23:57
使用AJAX帖子。
let myVar = 'Hello'
$.post('http://localhost:5000/getjs', {myVar}, function(){
// Do something once posted.
})
你的瓶子会是这样的
@app.route('/getjs', methods=['POST'])
def get_js():
if request.method == 'post':
js_variable = request.form
return js_variable
或者,您可以执行以下操作:
@app.route('/getjs/<variable>')
def get_js(variable):
js_variable = variable
return js_variable
所以当你把你的网址指向苹果的时候,js_variable将会是‘http://localhost:5000/getjs/apples’
发布于 2019-03-03 05:08:05
使用原生javascript 'fetch‘。
Ari Victor的解决方案需要一个外部库: jquery
var url = 'http://localhost:5000/getjs';
var data = {field_name: 'field_value'};
fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers:{
'Content-Type': 'application/json'
}
}).then(res => res.json())
.then(response => console.log('Success:', JSON.stringify(response)))
.catch(error => console.error('Error:', error));
https://stackoverflow.com/questions/54936206
复制相似问题