js code: $.ajax({ type : "GET", async : true, url : url, contentType: "text/html", dataType : "json", beforeSend : function(request) { request.setRequestHeader("X-Test-Token", "abc"); }, ... });
当我通过cherrypy.request.headers获取请求标头时,
我无法看到" X-Test-Token"在其中。
如何在cherrypy中获取自定义标题?
答案 0 :(得分:0)
你完全确定JS工作正常吗?
这个python代码:
from pprint import pformat
import cherrypy as cp
class Root:
@cp.expose
def default(self):
return pformat(cp.request.headers)
cp.quickstart(Root())
使用此curl命令:
curl -H "X-Test-Token: abc" http://localhost:8080
是否有效。
答案 1 :(得分:0)
这似乎是jQuery代码的问题。我使用JSFiddle将JS请求发送到RequestBin,看起来标题没有按预期发送。
RequestBin在这里:http://requestb.in/s5edg2s5?inspect
您可能必须使用新的RequestBin来测试该代码,因为它们已过期。无论如何,bin向我显示X-Test-Token标头未被发送,而是作为Access-Control-Request-Headers标头的内容发送,而不是作为实际标头发送。亲自尝试一下,你就会看到。
this SO post中介绍了设置自定义标头的正确方法。我认为你没有正确使用beforeSend回调。