[英]Flask WTForm as response to a AJAX call
在Flask應用程序中,我試圖根據各種按鈕選擇來“渲染”不同的Flask-WTForms。 我還需要更改div的內部HTML,因此嘗試按以下方式同時傳遞innerHTML和表單。 我收到表單無法JSON可序列化的錯誤。
關於如何通過AJAX調用而不是呈現新頁面的任何建議?
Page是來自下面描述的類的對象。
@app.route('/_select_forms',methods=['GET','POST'])
def _creation_frame():
content_requested=request.get_json()
Page=Page_contribute_frame(content_requested)
return jsonify(html=Page.html_string, form=Page.form)
class Page:
def __init__(self, **kwargs):
self.html_string = None
self.form = None
<div id=contribute-frame></div>
function contribute_frame(e){
var request_json = frame_identifier();
$.ajax({
type:'POST',
url:'/_contribute_frame',
contentType: "application/json",
data:request_json,
success : function(data){
$('#contribute-frame').html(data.html);
},
error : function (){
$('#contribute-frame').html('Browser did not get a response. Check connection.');
}
});
}````
通過呈現字符串在Flask中解決了此問題。
return render_template_string(Page.html_string, form=Page.form)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.