来自 jQuery Ajax 的 Bool 参数在 PHP 中作为文字字符串“false”/“true”接收
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7408976/
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
Bool parameter from jQuery Ajax received as literal string "false"/"true" in PHP
提问by Enrique
This question is related with:
这个问题与:
Cannot pass null to server using jQuery AJAX. Value received at the server is the string "null"
无法使用 jQuery AJAX 将 null 传递给服务器。服务器收到的值为字符串“null”
But I'm asking it again because the solution in that question is very ugly, and I think must be a better one.
但我再次问它,因为该问题的解决方案非常丑陋,我认为必须是更好的解决方案。
PROBLEM
When you send data with jQuery Ajax to PHP using POST you get strings "false"(string) instead of false(bool), "true" instead of true(bool) and "null" instead of NULL:
问题
当您使用 POST 使用 jQuery Ajax 将数据发送到 PHP 时,您会得到字符串“false”(string) 而不是 false(bool)、“true”而不是 true(bool) 和“null”而不是 NULL:
SOLUTION
(proposed in the above question):
convert the data to JSON before send it with jQuery, and then decode that data in PHP.
With code:
解决方案
(在上述问题中提出):在使用 jQuery 发送之前将数据转换为 JSON,然后在 PHP 中解码该数据。用代码:
Javascript code:
Javascript代码:
$.ajax
(
{
url : <server_url>,
dataType: 'json',
type : 'POST',
success : receiveAjaxMessage,
data : JSON.stringify
(
{
valueTrue : true,
valueFalse: false,
valueNull : null
}
)
}
);
PHP code:
PHP代码:
var_dump(json_decode(file_get_contents('php://input'), true));
To me that's not a solution, only an ugly hack.
对我来说,这不是一个解决方案,只是一个丑陋的黑客。
The problem in my situation is that I can't use file_get_contents('php://input') to access the $_POST, even more, I can't use $_POST because I'm using HMVC.
我的情况的问题是我不能使用 file_get_contents('php://input') 来访问 $_POST,更何况我不能使用 $_POST,因为我使用的是 HMVC。
And I know I can fix it checking those "false", "true" and "null" in PHP, or sending 1 and 0 instear true and false in jQuery.
But I think this must be a really common problem, and jQuery is a really common framework, so I'm pretty sure there's a better and elegant way to send the data with the right type in jQuery.
而且我知道我可以修复它在 PHP 中检查那些“false”、“true”和“null”,或者在 jQuery 中发送 1 和 0 instear true 和 false。
但我认为这一定是一个非常普遍的问题,而 jQuery 是一个非常普遍的框架,所以我很确定有一种更好、更优雅的方式来在 jQuery 中以正确的类型发送数据。
回答by skinneejoe
In my experience if you have
根据我的经验,如果你有
dataType: 'json'
and
和
contentType: 'application/json'
in your jQuery ajax call, and JSON.stringify your data before you send it will arrive at the server as a parse-able javascript object. Or in my case I'm using NodeJS serverside and it arrives as a Javascript object, with correct booleans, not string bools. But if I leave out the content type attribute then things get all goofed up including the bools.
在您的 jQuery ajax 调用中,并在发送之前对您的数据进行 JSON.stringify,它将作为可解析的 javascript 对象到达服务器。或者在我的情况下,我使用 NodeJS 服务器端,它作为一个 Javascript 对象到达,具有正确的布尔值,而不是字符串布尔值。但是,如果我省略了内容类型属性,那么包括 bool 在内的一切都会变得混乱。
回答by codelove
Because HTTP is a text protocol with no concept of booleans or integers everything must be stringified. Comparing strings on the server side is the normal way to deal with booleans.
因为 HTTP 是一个没有布尔值或整数概念的文本协议,所以一切都必须被字符串化。在服务器端比较字符串是处理布尔值的正常方法。
If you really really want PHP to see a 0 or 1 you can use
如果你真的想让 PHP 看到 0 或 1,你可以使用
valueTrue : true ? 1 : 0
in your AJAX call.
在您的 AJAX 调用中。
回答by Ariel
I ran into the same issue. I solved it by sending a 1 or 0 for booleans.
我遇到了同样的问题。我通过为布尔值发送 1 或 0 来解决它。
In the $.ajax
I did data : {field_name: field ? 1 : 0}
在$.ajax
我做的data : {field_name: field ? 1 : 0}
回答by Quaternion
If you will not handle the type conversion on the server then you must send what you expect.
如果您不会在服务器上处理类型转换,那么您必须发送您期望的内容。
Instead of:
代替:
{
valueTrue : true,
valueFalse: false,
valueNull : null
}
Use:
用:
{
valueTrue : 'TRUE',
valueFalse: 'FALSE',
valueNull : 'NULL'
}
A ternary operator is pretty clear...
三元运算符很清楚......
valueTrue : myvar ? : 'TRUE' : 'FALSE';
But probably clearer is to append ".toString().toUpperCase()" to the variables :
但可能更清楚的是将“.toString().toUpperCase()”附加到变量:
valueTrue : myvar.toString().toUpperCase();
However that will not work if myvar is null...
但是,如果 myvar 为空,那将不起作用......
This example shows a converter function you can use before sending data.
此示例显示了您可以在发送数据之前使用的转换器函数。
<html>
<head>
<script>
function myConverter(toUpper){
if (toUpper != null){
return toUpper.toString().toUpperCase();
}
return 'NULL';
}
//tests
alert(myConverter(null));
alert(myConverter(true));
alert(myConverter(false));
</script>
</head>
<body>
</body>
</html>
回答by loki_dev
I tried using Boolean object instead of primitive boolean type in the model at controller and it worked.
我尝试在控制器的模型中使用布尔对象而不是原始布尔类型,并且它起作用了。
I have contentType : 'application/json; charset=utf-8' and data type as "POST".
我有 contentType : 'application/json; charset=utf-8' 和数据类型为“POST”。