0

So I using cakephp and using ajax to submit forms. In some cases it returns a custom error.

For example, at one point it can be like:

Failed to load resource: the server responded with a status of 412 
([{"field":"PaymentCardholderName","message":"Please enter CardHolder's Name."},
{"field":"PaymentCardNumber","message":"Please enter Card Number."},
{"field":"PaymentCvvNumber","message":"Please enter CVV Number."},
{"field":"PaymentBillingAddress","message":"Please enter your Billing Address"}])

Here is the server logic in case anyone needs it

header('HTTP/1.1 412 ' . json_encode($error));

when I open this up locally I see my custom error message. When I deploy it to a remote server I just see my custom error message overwritten

Failed to load resource: the server responded with a status of 412 
(Precondition Failed)

It must be some configuration, but I can't find it.

3
  • 4
    Why on earth would you put data like this in the response line? Put it in the body of the response (echo it) - HTTP is not designed to work in the way you are attempting to use it. Commented Jan 18, 2012 at 10:12
  • @DaveRandom thanks for the response. I am trying to trigger a jquery ajax error . can u plz help me with that . how can i send the error in this case Commented Jan 18, 2012 at 11:02
  • Please add the jQ code that makes the request to your question so we can assist you with correct code Commented Jan 18, 2012 at 16:30

1 Answer 1

1

Additionally, instead of presuming HTTP/1.1

Do this:

header($_SERVER["SERVER_PROTOCOL"]." 412 Precondition Failed");

Instead of this:

header("HTTP/1.1 412 Precondition Failed");
header("HTTP/1.0 412 Precondition Failed");

Why? Because $_SERVER["SERVER_PROTOCOL"] gives you either HTTP/1.1 or HTTP/1.0 based on your browser. If you use the wrong one, apache might add unexpected content to the response. eg. a 4 digit hex code at the start (checksum?), and a trailing zero.

Sign up to request clarification or add additional context in comments.

1 Comment

And on top of that, the status line should never be relied on. It's intended as a human-readable string only. Code should only ever look at the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.