我正在尝试通过ajax请求将json发送到我的服务器。这是关于在jupyter实验室中嵌入的Web应用程序,我想将文件保存在云中而不是在本地保存,因此我需要将json发送到服务器,然后将其另存为json文件。
我在客户端尝试了ajax POST请求。
$.ajax({
url: '/discoverjson',
data: jsonString,
contentType: 'application/json;charset=UTF-8',
type: 'POST',
dataType: "json",
success: function(response) {
console.log(response)
},
error: function(error) {
console.log(error)
}
});
我正在龙卷风Web服务器中请求这样的回复:
class JSONHandler(tornado.web.RequestHandler):
def get(self):
if self.request.headers.get("Content-Type", "").startswith("/discoverjson"):
self.json_args = json.loads(self.request.body)
self.write(json_args)
else:
self.write('JSON Empty!')
self.json_args = None
如果我访问http://localhost:9999/discoverjson
,我将得到'JSON Empty!'
。问题是如果我要发送我收到的403 (Forbidden)
的json。
我没有足够的背景,也许这不是最好的方法。
有什么想法吗?
编辑
我在self.request.headers.get("Content-Type", "").startswith("/discoverjson")
中更改了此语法self.request.headers.get("Content-Type", "")
,但仍然禁止访问。
客户端控制台:
403 (Forbidden)
服务器端控制台:
[W 11:14:38.263 LabApp] 403 POST /discoverjson (172.21.0.1): '_xsrf' argument missing from POST
[W 11:14:38.263 LabApp] 403 POST /discoverjson (172.21.0.1) 0.92ms referer=http://localhost:9999/discover/index.html?
答案 0 :(得分:1)
问题似乎出在您的if语句
if self.request.headers.get("Content-Type", "").startswith("/discoverjson")
让我们把它分解一下
self.request.headers.get("Content-Type","")
这将返回application/json;charset=UTF-8
,因为这是您在Javascript中设置的。
所以它永远不会以/discoverjson
开头