Javascript Flask中的多个POST请求
我需要在我的WebApp的一个页面上使用Python+JS发出多个POST请求 以下是我的App.py代码:Javascript Flask中的多个POST请求,javascript,python,flask,Javascript,Python,Flask,我需要在我的WebApp的一个页面上使用Python+JS发出多个POST请求 以下是我的App.py代码: import json from flask import Flask, render_template, request, jsonify import requests app = Flask(__name__) @app.route("/",methods=['GET','POST']) def home(): if request.method == 'POST':
import json
from flask import Flask, render_template, request, jsonify
import requests
app = Flask(__name__)
@app.route("/",methods=['GET','POST'])
def home():
if request.method == 'POST':
#user inputs
value1 = request.form.get('first')
value2 = request.form.get('second')
value3 = request.form.get('third')
#api call
url = 'http://myapiurl.com/myapi/'
payload = {
"perfid" : value1,
"section" : {
"hostname" : value2,
"iteration" : value3,
"sectionname" : "sysstat_M"
}
}
r = requests.post(url, data=json.dumps(payload))
returnData = {}
if r.status_code == 200:
returnData["status"] = "SUCCESS"
returnData["result"] = json.loads(r.text)
return jsonify(returnData)
else:
returnData["status"] = "ERROR"
return jsonify(returnData)
#print(r.status_code, r.headers['content-type'])
#print(r.text)
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
现在我需要在同一个页面上有多个这样的请求后API调用。
多个POST请求意味着我需要在同一页面中使用多个API
例如:我有另一个API,我也需要使用相同的POST请求,因为只有一个参数不同,结果也会不同
url = 'http://myapiurl.com/myapi2/'
payload = {
"perfid" : value1,
"section" : {
"hostname" : value2,
"iteration" : value3,
"sectionname" : "DIFFERENT VALUE"
}
}
r = requests.post(url, data=json.dumps(payload))
returnData = {}
以下是我的JS代码:
$(document).ready(function() {
console.log("ready!");
$('form').on('submit', function() {
console.log("the form has beeen submitted");
// grab values
valueOne = $('input[name="perfid"]').val();
valueTwo = $('input[name="hostname"]').val();
valueThree = $('input[name="iteration"]').val();
console.log(valueOne)
console.log(valueTwo)
console.log(valueThree)
$.ajax({
type: "POST",
url: "/",
dataType:'json',
data : { 'first': valueOne,'second': valueTwo,'third': valueThree},
success: function(data) {
var x = parseInt(data.result.sectoutput.summarystats.Avg.AVG);
if(x>80)
{
var res = data.result.sectoutput.summarystats.Avg.AVG;
var p = '<p>CPU may be overloading.</p>';
$('#result').append(p);
}
else
{
var p = '<p>Normal Usage going on.</p>';
$('#result').append(p);
}
},
error: function(error) {
console.log(error)
}
});
});
});
$(文档).ready(函数(){
console.log(“准备就绪!”);
$('form')。在('submit',函数(){
console.log(“表格已提交”);
//抓住价值
valueOne=$('input[name=“perfid”]”)。val();
valueTwo=$('input[name=“hostname”]”)。val();
valueThree=$('input[name=“iteration”]”)。val();
console.log(valueOne)
console.log(valueTwo)
console.log(值三)
$.ajax({
类型:“POST”,
网址:“/”,
数据类型:'json',
数据:{'first':valueOne,'second':valueTwo,'third':valueThree},
成功:功能(数据){
var x=parseInt(data.result.sectoutput.summarystats.Avg.Avg);
如果(x>80)
{
var res=data.result.sectoutput.summarystats.Avg.Avg;
var p='CPU可能过载。';
$('#result')。追加(p);
}
其他的
{
var p='正在进行的正常使用。';
$('#result')。追加(p);
}
},
错误:函数(错误){
console.log(错误)
}
});
});
});
有人能帮我做这件事吗?
这可能吗
或者有人能告诉我在哪里我能理解如何处理多个POST请求吗?
谢谢。如果客户机必须打两个电话,jQuery可以打两个电话给您。它被称为deferred.then()。看看这个链接和 基本上,您将有两个ajax调用,而不是一个ajax调用,第一个调用将等待第二个调用完成,然后您可以在html中公开组合的数据。您将调用第一个API。成功后,将调用第二个API,最后将合并来自两个请求的数据并在屏幕上显示。语法类似于:
var request = $.ajax( "http://myapiurl.com/myapi/", { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( "http://myapiurl.com/myapi2/", { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
@app.route("/")
def your_method_name():
#get the data from the current first API
data = {
"perfid" : value1,
"section" : {
"hostname" : value2,
"iteration" : value3,
"sectionname" : "FIRST VALUE"
}
}
#then call the second api
r = requests.post('http://myapiurl.com/myapi2/')
data2 = json.loads(r.text) #this should give you the second payload with the different value if the call to the second API is successful
#combine data and data2 in a lsit
list = []
list.append(data)
list.append(dat2)
#return the combined data
return jsonify(list)
另一个更简洁的选择是使用jQuery“post”延迟函数,而不是ajax,如下所示:
$.post( "http://myapiurl.com/myapi/",
function( response1 ) {
$.post( "http://myapiurl.com/myapi2/",
function( response2 ) {
//combine your data here and display it
var result = []
result.append(response1)
result.append(response2)
$(".result").html(result);
}
);
}
);
如果需要第一个API在将数据传递给客户端之前调用第二个API,那么另一个选项是使用请求调用第一个API中的第二个API。比如:
var request = $.ajax( "http://myapiurl.com/myapi/", { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( "http://myapiurl.com/myapi2/", { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
@app.route("/")
def your_method_name():
#get the data from the current first API
data = {
"perfid" : value1,
"section" : {
"hostname" : value2,
"iteration" : value3,
"sectionname" : "FIRST VALUE"
}
}
#then call the second api
r = requests.post('http://myapiurl.com/myapi2/')
data2 = json.loads(r.text) #this should give you the second payload with the different value if the call to the second API is successful
#combine data and data2 in a lsit
list = []
list.append(data)
list.append(dat2)
#return the combined data
return jsonify(list)
如果客户端必须执行这两个调用,那么jQuery可以执行两个调用。它被称为deferred.then()。看看这个链接和 基本上,您将有两个ajax调用,而不是一个ajax调用,第一个调用将等待第二个调用完成,然后您可以在html中公开组合的数据。您将调用第一个API。成功后,将调用第二个API,最后将合并来自两个请求的数据并在屏幕上显示。语法类似于:
var request = $.ajax( "http://myapiurl.com/myapi/", { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( "http://myapiurl.com/myapi2/", { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
@app.route("/")
def your_method_name():
#get the data from the current first API
data = {
"perfid" : value1,
"section" : {
"hostname" : value2,
"iteration" : value3,
"sectionname" : "FIRST VALUE"
}
}
#then call the second api
r = requests.post('http://myapiurl.com/myapi2/')
data2 = json.loads(r.text) #this should give you the second payload with the different value if the call to the second API is successful
#combine data and data2 in a lsit
list = []
list.append(data)
list.append(dat2)
#return the combined data
return jsonify(list)
另一个更简洁的选择是使用jQuery“post”延迟函数,而不是ajax,如下所示:
$.post( "http://myapiurl.com/myapi/",
function( response1 ) {
$.post( "http://myapiurl.com/myapi2/",
function( response2 ) {
//combine your data here and display it
var result = []
result.append(response1)
result.append(response2)
$(".result").html(result);
}
);
}
);
如果需要第一个API在将数据传递给客户端之前调用第二个API,那么另一个选项是使用请求调用第一个API中的第二个API。比如:
var request = $.ajax( "http://myapiurl.com/myapi/", { dataType: "json" } ),
chained = request.then(function( data ) {
return $.ajax( "http://myapiurl.com/myapi2/", { data: { user: data.userId } } );
});
chained.done(function( data ) {
// data retrieved from url2 as provided by the first request
});
@app.route("/")
def your_method_name():
#get the data from the current first API
data = {
"perfid" : value1,
"section" : {
"hostname" : value2,
"iteration" : value3,
"sectionname" : "FIRST VALUE"
}
}
#then call the second api
r = requests.post('http://myapiurl.com/myapi2/')
data2 = json.loads(r.text) #this should give you the second payload with the different value if the call to the second API is successful
#combine data and data2 in a lsit
list = []
list.append(data)
list.append(dat2)
#return the combined data
return jsonify(list)
我真的不知道你想在这里做什么。你能试着换一种解释吗?我真的不知道你在这里想做什么。你能试着换一种解释吗?