cam*_*enl 4 python ajax jquery flask
我希望得到这个类似问题的答案:
我有一个 jQuery 表单,它将 JSON 数据发送到由 Flask 提供的某个 URL:
<div id="form">
<form id="send">
<input type="submit" value="Continue" >
</form>
</div>
jQuery(document).on('ready', function() {
jQuery('form#send').bind('submit', function(event){
event.preventDefault();
var form = this;
json = geojson.write(vectors.features);
$.ajax({
type: "POST",
contentType: 'application/json',
url: $SCRIPT_ROOT + "/create",
dataType: "json",
success: function(){},
data: json
});
});
});
Run Code Online (Sandbox Code Playgroud)
在 Flask 方面,我有一个简单的函数来呈现一个显示 JSON 对象的 HTML 页面:
@app.route('/create', methods=['POST'])
def create():
content = request.json
print content
return render_template('string.html', string = content)
Run Code Online (Sandbox Code Playgroud)
我知道 JSON 正在传递,因为我可以看到它打印在运行 Flask 服务器的控制台中:

问题是模板作为 Ajax 请求响应的一部分呈现,但我希望模板呈现为新的 html 页面:

小智 7
尝试这个,
$.ajax({
type: "POST",
contentType: 'application/json',
url: $SCRIPT_ROOT + "/create",
dataType: "json",
success: function(){},
data: json
success:function(response){
document.write(response);
}
});
Run Code Online (Sandbox Code Playgroud)