<p>我知道有点晚了,但我和你有同样的问题。</p>
<p>我需要用Ajax发布一篇文章,如果成功的话,我需要向flask发出GET请求以返回一个呈现的模板。类似于:</p>
<p>烧瓶部分:</p>
<pre><code>@app.route('/something', methods=['POST'])
def something_post():
# Get JSON object passed with Ajax request
elems = request.json
# Do stuff
# If everithing is OK, return success message
return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}
@app.route('/something', methods=['GET'])
def something_get():
# Return template
return render_template('something.html')
</code></pre>
<p>AJAX部分:</p>
<pre><code>$.ajax({
type: 'POST',
data: myJsonString, // JASON object passed to flask
url: '/something',
contentType: "application/json",
method: 'POST',
success: function(response) {
# If we succed we make a request to get the template
window.location.href = "/something";
# With this line we are making a simple GET request to Flask and the template returned is opened in the current window
}
});
</code></pre>
<p>请注意,我离成为Ajax专家还很远很远。我不知道这样做是否正确,但这是我找到的最简单的方法。</p>