【发布时间】:2019-07-17 02:55:07
【问题描述】:
我正在尝试通过将 python 函数与 XMLHttpRequest() 接口来将从后端 python 函数返回的值显示到 html 表中。 1) 该表适用于没有 python 函数返回值的 javascript 数组。 2) 当 javascript 变量传递给 html 正文中的元素 id 时,python 函数调用起作用。 3)当python返回值被传递给javascript数组以显示在表中时,它会破坏表。谢谢。
模块
class GetName:
...
def get_name():
return "Johnny Test"
烧瓶服务器
...
@app.route('/record_status', methods=['POST'])
def get_value():
json = request.get_json()
status = json['status']
if status == "true":
res = GetName.get_name()
return jsonify(result=res)
var myJSON;
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
// XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
//var myObj = JSON.parse(this.responseText);
var myObj = JSON.parse(xhr.responseText);
myJSON = JSON.stringify(myObj);
document.getElementById("demo").innerHTML = myJSON;
// alert(xhr.responseText);
}
};
xhr.open("POST", "/record_status");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify({ status: "true" }));
//function GenerateTable() {
//Build an array containing Customer records.
var customers = new Array();
customers.push(["Customer Id", "Name", "Country"]);
customers.push([1, 'John smith', "United States"]);
customers.push([2, "Anita Ross", "Canada"]);
customers.push([3, myJSON, "Mexico"]);
条件 1) 和 2) 处于正常工作状态。一旦将 var myJSON 插入数组中,表格就会中断
【问题讨论】:
标签: javascript python flask