我试图在单击删除按钮时从前端向后端发送一个字符串,但是后端返回状态码500:View函数未返回响应
我尝试使用request.get_data,request.get_json,尝试从前端发送一个字符串并发送一个数组
javascript
const ip = '192.168.0.116';
const backend_IP = `http://${ip}:5000`;
const backend = backend_IP + '/api/v1';
const listenToTrashes = function() {
document
.querySelector('.js-table')
.addEventListener('click', function(event) {
const delButton = event.target.closest('.js-listendelete');
if (delButton && this.contains(delButton)) {
const firstCell = delButton.closest('tr').querySelector('td');
if (firstCell) {
console.log(firstCell.innerHTML);
const data = firstCell.innerHTML;
const body = `{"htmlvalue" : "${data}"}`;
handleData(backend + '/table', next, 'POST', body);
}
}
});
};
DataHandler
const handleData = function(url, callback, method = 'GET', body = null) {
fetch(url, {
method: method,
body: body,
headers: { 'content-type': 'application/json' }
})
.then(function(response) {
if (!response.ok) {
throw Error(`Probleem bij de fetch(). Status Code: ${response.status}`);
} else {
console.info('Er is een response teruggekomen van de server');
return response.json();
}
})
.then(function(jsonObject) {
console.info('json object is aangemaakt');
console.info('verwerken data');
callback(jsonObject);
})
.catch(function(error) {
console.error(`fout bij verwerken json ${error}`);
});
};
后端
@app.route(endpoint + "/table", methods=["GET", "POST"])
def db_to_table():
if request.method == "GET":
antw_db = conn.get_data("SELECT f.products_barcode, f.date, f.expirationDate, f.amount, p.name FROM fridge_has_products AS f left join products AS p ON f.products_barcode = p.barcode ORDER BY f.date DESC")
json_db = jsonify(antw_db)
return json_db
elif request.method == "POST":
title = request.get_json()
print(title)
我现在只需要将前端的值打印在后端即可
答案 0 :(得分:1)
在后端,POST请求没有返回值。
尝试返回一些字符串。
例如return 'test'
,return jsonify(title)
等