3

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()}})

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');
});

and

$.ajax({
  url:'http://localhost:8080/',
  type: "POST",
  crossDomain: true,
  dataType: "jsonp",
  data: {command:"version"}
}).done(function(){
  console.log('hej');
});

and

$.support.cors = true

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 the server is perfectly ok, since all curl tests pass. Something is wrong with the cross-domain stuff.

ducin
  • 25,621
  • 41
  • 157
  • 256
  • What is "the browser"? – Kevin B Oct 26 '13 at 22:01
  • it's chrome 27.0.1453.93 – ducin Oct 26 '13 at 22:02
  • 1
    crossDomain: true and $.support.cors = true are definitely not needed, and you don't want jsonp unless your server is returning jsonp (at which point you wouldn't need cors). it also isn't possible to send a POST jsonp request because jsonp is sent using a script tag rather than xhr. Your first snippet is correct. Can you post the request and response headers being sent? (chrome console) – Kevin B Oct 26 '13 at 22:04
  • @KevinB but when I execute `$.ajax({ url:'http://localhost:8080/', type: "POST", data: {command:"version"} }).done(function(){ console.log('hej'); });` I get the error: `XMLHttpRequest cannot load http://localhost:8080/. Origin http://localhost is not allowed by Access-Control-Allow-Origin. ` – ducin Oct 26 '13 at 22:06
  • that's fine. but making any of those changes won't change the outcome. your javascript is fine. the problem is more likely to be either browser permissions(very unlikely) or the server just isn't handling the cors request correctly. – Kevin B Oct 26 '13 at 22:07
  • You were right. The problem was that my server was not sending proper headers (I debugged with wget/curl). – ducin Oct 30 '13 at 08:30

1 Answers1

1

Are you activating the CORS tool?. You can use the tool by decorating the calling methods or set it on the configuration.

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}})

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.

cyraxjoe
  • 5,661
  • 3
  • 28
  • 42