【发布时间】:2018-07-18 14:19:40
【问题描述】:
我使用 Flask 对我的 REST API 进行了以下 ajax 调用:
$.ajax({
type: "POST",
cache: false,
url: "/addEvent",
dataType: "json",
data: JSON.stringify({
"event_id": "e-123"
}),
success: function(response, status, xhr) {
var ct = xhr.getResponseHeader("content-type") || "";
if ( xhr.status == 200 ) {
console.log("Event created.");
}
else {
alert("Error! status: " + xhr.status)
}
},
error: function(xhr, exception) {
console.log("error > xhr.status:" + xhr.status);
console.log("error > exception: " + exception);
},
complete: function(response, status, xhr) {
console.log("Finished!");
}
});
我的 Flask REST API app.py 文件包含以下内容:
from flask import Flask, request
def addEvent():
request_data = request.get_json()
但是,'request_data' 是 None,而 request.get_data() 包含 b'{"event_id":"e-123"}'
我的 ajax 调用有什么问题吗?
【问题讨论】: