vsailas has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am inside Catalyst controller, which responds to an ajax call, there is an situation that server dont like and want to send some custom message as part of response to browser, it cannot be res->status(100, 200 ...).
Does catalyst have any feature to send some code to the Ajax call resolver, just like 'response.data' or 'response.status'

Many Thank,
-Sailas

Replies are listed 'Best First'.
Re: catalyst custom response
by tobyink (Canon) on Mar 05, 2012 at 11:49 UTC

    I'm not entirely sure what you're asking. HTTP status lines have two parts. The classic success response is:

    200 OK

    This is a code (200) followed by a reason (OK). HTTP doesn't impose any requirements on the reason, other than it must not contain line break or carriage return characters. (It may even be the empty string, but the space separating it from the status code must still be present.

    Anyway, what I think you are asking is, how can you customise the reason string in Catalyst? The answer is: you can't. This isn't Catalyst's fault, but is a limitation of the underlying Plack infrastructure.

    I'd suggest that instead you put your reason into an HTTP header. HTTP 1.1 defines a Warning header for this purpose, but it has quite specific syntax requirements. Using a custom header such as X-Warning may be preferred.

    Whichever header you choose, you'll want to check that your AJAX framework (e.g. jQuery) exposes it to you. If this is a cross-origin request, then also be aware that browsers sometimes refuse to expose some headers. CORS offers some control over this behaviour.

      You got it right, I need a custom string as part of response.
      I don't mind going out of Catalyst for this, thanks for pointing HTTP headers to me, shall try this out.
      -Sailas
Re: catalyst custom response
by Anonymous Monk on Mar 05, 2012 at 10:58 UTC
      Yea, guess I have to live with what I see. Appreciate your help. -Sailas