Html 如何在 URL 中传递 false 的布尔变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29739970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:52:57  来源:igfitidea点击:

How to pass boolean variable of false in a URL

htmlget

提问by Benjen

I cannot work out how to pass a variable with a boolean value of false through a URL string. What is the correct method?

我无法弄清楚如何通过 URL 字符串传递布尔值为 false 的变量。正确的方法是什么?

  • domain/page?test=false
  • domain/page?test=
  • domain/page?test=0
  • 域/页面?测试=假
  • 域/页面?测试=
  • 域/页面?测试=0

回答by Jordan Running

URLs are strings and all values in a URL are strings. When you see i=0in a URL, 0is a string. When you see b=true, trueis a string. When you see s=, the value is an empty string.

URL 是字符串,URL 中的所有值都是字符串。当您i=0在 URL 中看到时,0是一个字符串。当你看到b=true,true是一个字符串。当您看到 时s=,该值是一个空字符串。

For those strings to take on another meaning—the integer 0or the Boolean true, say—the server-side program must be told how to interpret them. Often web frameworks will make intelligent guesses as to the right type, or sometimes a type is declared explicitly in a model.

为了让这些字符串具有另一种含义——比如整数0或布尔值true——必须告诉服务器端程序如何解释它们。通常,Web 框架会对正确的类型进行智能猜测,或者有时在模型中显式声明类型。

How you'll do it depends entirely on what server-side language and what framework, if any, you're using.

您将如何做完全取决于您使用的服务器端语言和框架(如果有)。

回答by sohan nohemy

It depends on how you interprete the the parameter string. You can write converter that can convert string values "false", "0", "" to false. You can take all approach, any one of them or mix.

这取决于您如何解释参数字符串。您可以编写可以将字符串值“false”、“0”、“”转换为 false 的转换器。您可以采用所有方法,其中任何一种或混合使用。

回答by brandon-estrella-dev

Like mentioned above, it's depends on the server-side language you are using as to how you will handle it. To add to the answer, if you want to pass a true or false in the URL as a parameter you can

如上所述,这取决于您使用的服务器端语言如何处理它。要添加到答案中,如果您想在 URL 中传递 true 或 false 作为参数,您可以

running node on server

在服务器上运行节点

const whatever = JSON.parse(queryStringParameter)

running php on server

在服务器上运行 php

$whatever = json_decode(queryStringParameter)

Both options will give you a boolean type that you can use on the server.

这两个选项都将为您提供一个可以在服务器上使用的布尔类型。