Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

php - When should I return true/false to AJAX and when should I echo "true"/"false"

Somehow I have confused myself.

Somehow I got it in my head that when hitting PHP with AJAX (like $.post), you had to echo back a "true" or "false" instead of returning true/false. I see now that is not the case, but can someone break it down for me?

Is it that there is a problem testing a boolean? Like here

... 
$.post('ajax/doThing',{data: data},
    function(response) {
        if(response) {
            doThis();
        }else{
            doThat();
        }

That is the problem case, correct? Here I cannot return true/false, instead I must echo back a string and test the string, yes?

if(response === "true")

But I have seen boolean true/falses returned to ajax functions. What is the use of this, if you cannot test a boolean on the AJAX side? And why can't ajax test a boolean?

Or am I still confused?

EDIT

Just wanted to thank everyone for their good answers on this. I am now +2 smrter.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. HTML (if it's going to populate page elements)

  2. JSON (if you want a lightweight, fast way to send data to the client)

  3. XML (if you want a heavier-weight, fast way to send data to the client)

  4. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...