Search code examples
pythonajaxcherrypy

CherryPy Returning Status 301 on nsIXMLHttpRequest (but works fine for firefox HTTP request)


I am developing a web API in using CherryPy. The intent is that it is accessed by JavaScript through nslXMLHttpRequest. When I access the API through Firefox (as though it were an ordinary-variety URL), the following appears in my logs:

!!!SUCCESS!!!
[my IP] - - [30/Sep/2011:08:30:19] "GET /myAPI/ HTTP/1.1" 200 11 "" "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2"

That "!!!SUCCESS!!!" is printed at the start of the code that runs on the myAPI page. But when I access it through JavaScript, the following appears in my logs:

[my IP] - - [30/Sep/2011:08:32:19] "GET /myAPI?arg1=value1&arg2=value2 HTTP/1.1" 301 221 "[requesting page]" "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2"

(those arguments are optional)

Notice that in the second case, !!!SUCCESS!!! was never printed (my code was never executed) and the status code was 301 - "moved permanently". Any idea what would cause CherryPy to do this?


Solution

  • Probably because /myAPI and /myAPI/ are not the same URI, so it's redirecting you from one to the other. You can fine-tune this behavior with http://docs.cherrypy.org/dev/refman/lib/cptools.html#cherrypy.lib.cptools.trailing_slash