我将使用dojo和ajax发送一个时间戳到PHP,用于进行数据库检查,以便于调试。当我发送这个时间戳时,它是一个数字,但当它被返回时,它变成了一个字符串。这是有特定原因的吗?我该如何避免这种情况(在PHP中强制转换为int,通过JSON修复或在javascript中强制转换为int)?
以下是Dojo代码:
最后,上面留空的加载函数。
其输出结果为:
以下是Dojo代码:
dojo.xhrGet({
url: 'database/validateEmail.php',
handleAs: "json",
content: {
email : 'George.Hearst@Pinkerton.dw',
time: 0
},
load: function(args) {/*SEE BELOW*/}
});
以下是 PHP 脚本:
<?php
/**
** connect to the MySQL database and store the return value in $con
*
*/
$con = mysql_pconnect("localhost:port", "username", "password");
/**
** handle exceptions if we could not connect to the database
*
*/
if (!$con) {
die('Could not connect: ' . mysql_error());
}
/**
** Create table query
*
*/
mysql_select_db("portal", $con);
/**
** Get user entered e-mail
*
*/
$emailQuerry = mysql_num_rows(mysql_query("SELECT EMAIL FROM user WHERE EMAIL='" . $_GET["email"] . "'")) == 1;
/**
** Whether successful or not, we will be returning the time stampe (this is used to determine whether there were any changes between the time a request
** was sent, and when this response was returned.
*
*/
$result['time'] = $_GET["time"];
/**
** Currently only checks to see if the two values were provided. Later, will have to check against passwords
*
*/
if ($emailQuerry) {
$result['valid'] = true;
}
else {
$result['valid'] = false;
}
echo json_encode($result);
?>
最后,上面留空的加载函数。
load: function(args) {
console.log(localArgs.time + ' v ' + args.time);
console.log(localArgs.time === args.time);
console.log(localArgs.time == args.time);
}
其输出结果为:
0 v 0
false
true
$result['valid'] = $emailQuerry;
来替换整个代码块:if ($emailQuerry) {... }
。 - Blender