Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript @app.route在没有重定向的情况下无法工作_Javascript_Python_Html_Flask - Fatal编程技术网

Javascript @app.route在没有重定向的情况下无法工作

Javascript @app.route在没有重定向的情况下无法工作,javascript,python,html,flask,Javascript,Python,Html,Flask,如果我重定向到另一个页面@app.route('/results'),则此函数工作正常,但我很难在不重新加载的情况下使其在同一页面中工作: @app.route('/') def static_file(): return app.send_static_file('index.html') @app.route('/') def results(): headers = { 'X-ApiKey': 'xxxxxxxxxxxxxxxx', }

如果我重定向到另一个页面@app.route('/results'),则此函数工作正常,但我很难在不重新加载的情况下使其在同一页面中工作:

@app.route('/')
def static_file():
    return app.send_static_file('index.html')

@app.route('/')
def results():
    headers = {
        'X-ApiKey': 'xxxxxxxxxxxxxxxx',
    }

    data = {'data': request.args.get('data')}

    results = requests.post('https://apiv2.indico.io/personality', headers=headers, data=data)
    return results.text
我希望在同一页面(输入框下方)中显示api调用的结果


试试看




您的结果:

我还添加了一些JS:

 <script language="JavaScript">
    function showResults() {
        document.getElementById('display').innerHTML = 
                    document.getElementById("Results").value;
    }
  </script>

函数showResults(){
document.getElementById('display')。innerHTML=
document.getElementById(“结果”).value;
}

我的代码怎么了?我使用的是Flask框架。

如果我理解正确,您希望实现的是在Flask微框架中编写RESTful API,这将允许您获得一些结果并在页面上显示,而无需重新加载。如果是这样,你的方法是错误的

首先,您不能使用公共URL路由多个函数:

@app.route('/common_route')
def foo():
    pass
@app.route('/common_route')
def bar():
    pass
如果加载,则无法确定要调用哪个函数

为了避免页面重新加载,您应该使用Flask RESTful模块并实现一个API来获得结果

import flask-restful
class Results(Resource):
    def __init__(self):
        super(Results)
    #for GET requests
    def get(self):
        #your code here
        pass
    #for POST requests
    def post(self):
        #your code here
        pass

api = Api(app)
api.add_resource(Results(),'/api/results')
然后您可以使用JS&jQuery代码获得您的f.e.结果:

当我使用@app.route('/result')时,我在一个空页面中获得了api结果(Json),我希望在同一页面(索引)中获得此输出。我试过Jquery和Ajax。我在google上找到的所有示例都使用不同类型的表单或显示(用户输入)
import flask-restful
class Results(Resource):
    def __init__(self):
        super(Results)
    #for GET requests
    def get(self):
        #your code here
        pass
    #for POST requests
    def post(self):
        #your code here
        pass

api = Api(app)
api.add_resource(Results(),'/api/results')