什么时候应该返回true / false来AJAX,当我要呼应"真" /"假" [英] When should I return true/false to AJAX and when should I echo "true"/"false"

查看:131
本文介绍了什么时候应该返回true / false来AJAX,当我要呼应"真" /"假"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知怎的,我搞糊涂我自己。

Somehow I have confused myself.

不知怎的,我得到了它在我的头上,随着AJAX击中PHP时(如$。员额),你必须回显真或假,而不是返回真/假。我现在看到的情况并非如此,但有人可以打破它给我吗?

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")

不过,我所看到的布尔值true / falses回到了AJAX功能。什么是使用这个,如果你不能测试的AJAX端的布尔?为什么不能阿贾克斯测试一个布尔值?

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?

还是我仍然感到困惑?

修改

只是想感谢每一位为这个好答案。我现在+2 smrter。

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

推荐答案

您可能还要查看返回HTTP错误codeS而不是返回一个成功的响应(HTTP状态code 200)当请求WASN 吨真的成功,然后使用错误回调处理不成功的请求。

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.

但是,如果你想继续使用状态code 200(和很多人这样做):

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(如果它要来填充页面元素)

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

JSON(如果希望轻便,快速的方式将数据发送到客户端)

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

的XML(如果你想较重权重,快速的方式将数据发送到客户端)

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

纯文本(为任何你想要的,真的)

Plain text (for whatever you want, really)

什么是客户端不会将取决于你在你的PHP页面使用什么内容类型头。

What the client does will depend on what Content-Type header you use in your PHP page.

我的猜测是,你正在使用的任何最终传递的数据作为一个字符串回调几种内容类型。字符串真实是truthy,但这样是在字符串 (只有空白的字符串falsey)。

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

长话短说:我可能会使用这在我的PHP:

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}

{"success": false}

...,然后在你的成功处理程序:

...and then in your success handler:

if (response.success) {
    // It was true
}
else {
    // It was false
}

另外,也可以返回内容类型文本/纯和使用

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.

这篇关于什么时候应该返回true / false来AJAX,当我要呼应"真" /"假"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆