cherrypy / jquery CORS麻烦 [英] cherrypy/jquery CORS trouble
问题描述
我有一个基于cherrypy的简单的python web服务器。其资源应提供一个API。服务器有以下代码来提供CORS:
I've got a simple python web server based on cherrypy. Its resources shall provide an API. THe server has the following code to provide CORS:
def CORS():
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
if __name__ == "__main__":
cherrypy.tools.CORS = cherrypy.Tool('before_finalize', CORS)
cherrypy.quickstart(PyCachedAdmin(), config={'/': {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}})
服务器正在localhost:8080上运行。现在我有一个HTML文件,可在localhost(默认端口80)加载jquery 1.9。我打开浏览器控制台尝试 $。ajax
来向cherrypy服务器执行任何AJAX请求。我一直在尝试:
the server is running on localhost:8080. Now I've got a HTML file, available on localhost (default port 80) which loads jquery 1.9. I open the browser console to try the $.ajax
to execute any AJAX request to the cherrypy server. I've been trying:
$.ajax({
url:'http://localhost:8080/',
type: "POST",
dataType: "json",
data: {command:"version"}
}).done(function(){
console.log('hej');
});
和
$.ajax({
url:'http://localhost:8080/',
type: "POST",
crossDomain: true,
dataType: "jsonp",
data: {command:"version"}
}).done(function(){
console.log('hej');
});
和
$.support.cors = true
我得到 XMLHttpRequest不能加载http:// localhost:8080 /。原始http:// localhost不被Access-Control-Allow-Origin允许。
或 GET http:// localhost:8080 /?callback = jQuery19102827550224028528_1382823727186& command = version& _ = 1382823727187 404(找不到)
时使用jsonp(它是神秘的,它发送GET而不是POST)。有几个类似的问题,我试过他们,这些都是我的结果(有些仍然是错误的)。
and nothing worked. I'm getting either XMLHttpRequest cannot load http://localhost:8080/. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
or GET http://localhost:8080/?callback=jQuery19102827550224028528_1382823727186&command=version&_=1382823727187 404 (Not Found)
when using jsonp (it's mysterious that it sends GET instead of POST). There is a few similar questions around, I tried them and these are my results (that something is still wrong).
PS服务器是完全确定,因为所有卷曲测试通过。
PS the server is perfectly ok, since all curl tests pass. Something is wrong with the cross-domain stuff.
推荐答案
您是否正在启用CORS工具?您可以通过装饰调用方法或在配置上设置它来使用该工具。
Are you activating the CORS tool?. You can use the tool by decorating the calling methods or set it on the configuration.
由于 PyCachedAdmin
的实现没有表示的问题,我可能猜到,可能你没有启用工具,这样做只需要更改配置字典,并像这样:
Given that the implementation of PyCachedAdmin
is no expressed on the question I might guess that probably you are not enabling the tool, to do so you just need to change the config dictionary and make something like this:
cherrypy.quickstart(PyCachedAdmin(),
config={
'/': {
'request.dispatch':
cherrypy.dispatch.MethodDispatcher(),
'tools.CORS.on': True}})
或者如果你使用的方法 PyCacheAdmin
已经装饰或使用 _cp_config
,不需要额外的配置,这个答案不会帮助你。
Or if the methods that you are using on PyCacheAdmin
has already been decorated or using _cp_config
that extra configuration is not required and this answers will not help you.
这篇关于cherrypy / jquery CORS麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!