在flask中通过响应,将json数据显示在网页中,并且将Content-Type该为application/json格式

1,第一种方法:

from flask import jsonify

@app.route("/index")
def index():
data = {"name":"zeng", "age":23}
return jsonify(data)

2,第二种方法:

import json

@app.route("/index")
def index():
data = {"name":"zeng", "age":23}
json_str = json.dumps(data)
return json, 200, {"Content-Type": "application/json"}

,3,第三种方法:

import json

@app.route("/index")
def index():
data = {"name":"zeng", "age":23}
json_str = json.dumps(data)
resp = make_response(json_str)
resp.headers["Content-Type"] = "application/json"
return resp
05-11 16:11