好吧,我要读点书。我不指望有人再回答了。谢谢大家的回复。你知道吗
编辑:
好吧,在问这个问题之前我应该多读点东西。让我试着重新表述一下我想做的事。你知道吗
我正在尝试访问下面定义的html页面。我想在文本框中输入文本,然后在flask中定义的api函数中处理该文本。然后我想在html页面中输出的结果。
我试图找到更多的资源,但我是新的html和烧瓶和挣扎。
任何提示都会被告知。你知道吗
我的应用程序类型文件:from flask import Flask, request, jsonify
from flask_cors import CORS
from serve import get_model_api
app2 = Flask(__name__)
CORS(app2) # needed for cross-domain requests, allow everything by default
model_api = get_model_api()
# default route
@app2.route('/')
def index():
return "Index API"
# HTTP Errors handlers
@app2.errorhandler(404)
def url_error(e):
return """
Wrong URL!
{}""".format(e), 404
@app2.errorhandler(500)
def server_error(e):
return """
An internal error occurred:
{}
See logs for full stacktrace.
""".format(e), 500
# API route
@app2.route('/api', methods=['POST'])
def api():
input_data = request.json
output_data = model_api(input_data)
response = jsonify(output_data)
return response
if __name__ == '__main__':
app2.run(host='0.0.0.0', port=81, debug=True)
html文件:
你知道吗
Find Entities
Get random Tweet
function api_call(input) {
// hide button and make the spinner appear
$('#btn').toggle();
$('#spinner').toggle();
$.ajax({
url: "0.0.0.0:81/api",
method: 'POST',
contentType: 'application/json',
data: JSON.stringify(input),
success: function( data, textStatus, jQxhr ){
// toggle the spinner and button
$('#btn').toggle();
$('#spinner').toggle();
// fill the html for answer
$('#api_input').html( data.input );
$('#api_output').html( data.output );
$("#input").val("");
},
error: function( jqXhr, textStatus, errorThrown ){
$('#btn').toggle();
$('#spinner').toggle();
$('#api_input').html( "Sorry, the server is asleep..." );
$('#api_output').html( "Try again in a moment!" );
console.log( errorThrown );
},
timeout: 3000 // sets timeout to 10 seconds
});
}
$( document ).ready(function() {
// request when clicking on the button
$('#btn').click(function() {
// get the input data
var input = $("#input").val();
api_call(input);
input = "";
});
});