[英]GET request not in json content type flask
我正在嘗試在網站上的一條路線上以 json 格式獲取 sqlite 數據庫的數據,這按預期工作,如果我要訪問該鏈接,它在 chrome devtools 的網絡選項卡中顯示內容類型是應用程序/ json。 這當然是我想要的,除非我用 javascript 向這條特定路線發出 XML GET 請求,它說數據類型:文本/html。 有誰知道如何解決這一問題? python flask 代碼:
@app.route('/dossiers/all', methods=['GET'])
@login_required
def get_all_dosssiers():
ids = executeQueryResult('SELECT dossierid FROM dossiers', [])
# return jsonify(ids)
print(ids[1].get('DossierId'))
results = []
for id in ids:
results.append(get_dossier(id.get('DossierId')))
return jsonify(results)
JS:
var request = new XMLHttpRequest()
request.open('GET', `http://127.0.0.1:5000/dossiers/all`, true)
request.onload = function () {
// Begin accessing JSON data here
var data = JSON.parse(this.response)
if (request.status >= 200 && request.status < 400) {
data.forEach((dossier) => {
const card = document.createElement('button')
card.setAttribute('class', 'card')
const kruisje = document.createElement('button')
kruisje.setAttribute('class', 'kruisje')
const h1 = document.createElement('h1')
h1.textContent = dossier.ziekte
const p = document.createElement('p')
dossier.description = dossier.description.substring(0, 300)
p.textContent = `${dossier.behandeling}...`
const id = dossier.dossierId
container.appendChild(card)
card.appendChild(kruisje)
card.appendChild(h1)
card.appendChild(p)
card.addEventListener("click", function() {
window.location = `dossier.html?id=${dossier.id}`
})
})
} else {
const errorMessage = document.createElement('p')
errorMessage.textContent = `Het werkt niet...`
app.appendChild(errorMessage)
}
}
它可以做一些事情,很難從你的描述中確切地說出什么。
CORS 。 根據您是否將一個作為localhost
打開並請求另一個作為127.0.0.1
,您可能需要設置XMLHttpRequest.withCredentials
。
根據login_required
裝飾器的工作方式,當您未通過身份驗證時,您可能會擁有 HTML 登錄頁面。
如果您正在運行Flask < 0.11
,則jsonify
如果您將其傳遞給list
則不會。
在 0.11 版更改:添加了對序列化頂級 arrays 的支持。 這在古代瀏覽器中引入了安全風險。 請參閱JSON 安全性。
基本上,在獲取 JSON 資源時Array.prototype
覆蓋的漏洞,因為 Javascript 僅存在於古代瀏覽器中,並且根列表可以很好地使用。 pex
的幾個例子。 我在app.py
中有這個。
from flask import Flask, jsonify app = Flask(__name__) @app.route('/dossiers', methods=['GET']) def dossiers(): return jsonify([{'foo': 'bar'}]) if __name__ == '__main__': app.run()
pex "flask < 0.11" -- app.py
$ curl -v localhost:5000/dossiers * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 5000 (#0) > GET /dossiers HTTP/1.1 > Host: localhost:5000 > User-Agent: curl/7.47.0 > Accept: */* > * HTTP 1.0, assume close after body < HTTP/1.0 500 INTERNAL SERVER ERROR < Content-Type: text/html; charset=utf-8 < Content-Length: 290 < Server: Werkzeug/2.0.1 Python/3.7.10 < Date: Mon, 24 May 2021 13:05:12 GMT...
pex "flask >= 0.11" -- app.py
$ curl -v localhost:5000/dossiers * Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 5000 (#0) > GET /dossiers HTTP/1.1 > Host: localhost:5000 > User-Agent: curl/7.47.0 > Accept: */* > * HTTP 1.0, assume close after body < HTTP/1.0 200 OK < Content-Type: application/json < Content-Length: 16 < Server: Werkzeug/2.0.1 Python/3.7.10 < Date: Mon, 24 May 2021 13:07:18 GMT < [{"foo":"bar"}] * Closing connection 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.