Search code examples
ajaxplayframework-2.0http-options-method

Play! 2.0 easy fix to OPTIONS response for router catch-all?


Having some annoying issues making AJAX calls simply because almost every browser these days is making an OPTIONS call to the server before the actual AJAX call.

Since I am using Play! 2.0, is there any easy way to make a wildcard response to any route using the OPTIONS method?

For instance, in my routes do something like: OPTIONS /* controllers.Options.responseDef

Yes I am aware that the new Play! doesn't have a wildcard built-in, but there needs to be a solution for this since all browsers are increasingly calling OPTIONS before AJAX calls.


Solution

  • Not quite a wildcard, but you can use a route which spans several slash-segments:

    OPTIONS   /*wholepath     controllers.Options.responseDef(wholepath)
    OPTIONS   /               controllers.Options.responseDef
    

    It should match all the requests:

    OPTIONS    /a
    OPTIONS    /a/b
    OPTIONS    /a/b/c
    

    Note: that's from the top of my head, so maybe you'll need to polish it. I can't check it now by myself.

    Check the section Dynamic parts spanning several / of the manual.