You might also look at returning HTTP error codes rather than returning a "success" response (HTTP status code 200) when the request wasn't really successful, and then use an error
callback to handle unsuccessful requests.
But if you want to keep using status code 200 (and a lot of people do that):
The data transferred between the client and the server is always text. The trick is to make sure that the client and server agree on how the client should deserialize the text (transform it upon receipt). Typically you might return one of four things:
HTML (if it's going to populate page elements)
JSON (if you want a lightweight, fast way to send data to the client)
XML (if you want a heavier-weight, fast way to send data to the client)
Plain text (for whatever you want, really)
What the client does will depend on what Content-Type
header you use in your PHP page.
My guess is that you're using any of several content types that end up passing on the data as a string to your callback. The string "true"
is truthy, but so is the string "false"
(only blank strings are falsey).
Long story short: I'd probably use this in my PHP:
header('Content-Type', 'application/json');
...and the return this text from it:
{"success": true}
or
{"success": false}
...and then in your success handler:
if (response.success) {
// It was true
}
else {
// It was false
}
Alternately, you can return a Content-Type
of text/plain
and use
if (response === "true") {
// It was true
}
else {
// It was false
}
...but that's kind of hand-deserializing where you could get the infrastructure to do it for you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…