PHP json 编码在 javascript 中无法正确解码
PHP json encode not properly decoding in javascript
所以我遇到一个问题,即使用 php 编码的数据在 javascript 中无法正确解码。这是错误:
Error
JSON 看起来不错,但 javascript 抛出错误,我不知道为什么。
这是我的代码:
JS:
function requestData( url )
{
document.getElementById( "spinner" ).style.visibility = "visible";
var xhttp;
xhttp = new XMLHttpRequest( );
xhttp.onreadystatechange = function( ) {
if ( this.readyState == 4 ) {
if( this.status == 200 )
{
document.getElementById( "spinner" ).style.visibility = "hidden";
console.log( this.responseText );
return this.responseText;
}
else
{
document.getElementById( "spinner" ).style.visibility = "hidden";
document.getElementById( "errorsec" ).innerHTML = ":( An unknown error has happened and your request was canceled. Code: " + this.status;
return false;
}
}
};
xhttp.open( "GET", url, true );
xhttp.send( );
}
function handleSignup( )
{
var username = document.getElementById( "un" ).value; // Get text field values
var password = document.getElementById( "pw" ).value;
var requestObject = requestData( "../../FRAMEWORK/createaccount.php?name=" + username + "&password=" + password, false );
var returnObject;
if( requestObject != false )
{
requestObject = JSON.parse( requestObject );
console.log( returnObject.value );
}
}
PHP:
<?php
# REV 1.0.0
$name = $_GET[ "name" ]; # Get values to set
$password = $_GET[ "password" ];
$file = fopen( "../USER_STORE/userindex.json", "r" ); # Load user index
$accounts = json_decode( fread($file, filesize( "../USER_STORE/userindex.json" ) ), true );
fclose($file);
$alreadyExists = false; # Check to see if username already is in use
foreach($accounts as $val) {
if( $val == $name )
{
$alreadyExists = true;
}
}
if( !$alreadyExists ) # If username is not in use
{
$UUID = sizeof( $accounts ) + 1;
$toAppend = array( "username" => $name, "password" => hash( "sha256", $password ) ); # Create append list
mkdir( "../USER_STORE/" . $UUID ); # Append user data
$file = fopen( "../USER_STORE/" . $UUID . "/accountinfo.json", "w" );
fwrite( $file, json_encode( $toAppend ) );
fclose( $file );
$accounts[ $UUID ] = $name; # Create new user index
$file = fopen( "../USER_STORE/userindex.json", "w" ); # Update userindex
fwrite( $file, json_encode( $accounts ) );
fclose( $file );
$return = array( "value" => "created" ); # Return message
echo( json_encode( $return ) );
}
else # Account is in use
{
$return = array( "value" => "account_in_use" ); # Return error message
echo( json_encode( $return ) );
}
?>
XMLHttpRequest
异步工作。所以你的函数 requestData
在 xhttp.send();
之后结束并且没有 return 任何东西。 readystatechange
的事件处理程序(稍后调用,一旦 XMLHttpRequest
实际收到来自服务器的响应)执行 return 一个值,但什么也没做。
解决方案:在该事件处理程序中移动响应的解析和解释。
补充说明:
构建URL时,需要对参数进行转义(使用encodeURIComponent
)。如果这两个字段中的任何一个包含空格或特殊字符,例如 %
或 &
,您认为会发生什么情况?
当你对密码进行哈希处理时,你必须先对其进行加盐处理。或者使用现成的函数,例如 password_hash
,它将为您完成。
您可能想使用 file_get_contents
/ file_put_contents
到 read/write 您的用户文件,这将大大简化您的代码。或者更好的是,一个数据库。
所以我遇到一个问题,即使用 php 编码的数据在 javascript 中无法正确解码。这是错误: Error
JSON 看起来不错,但 javascript 抛出错误,我不知道为什么。
这是我的代码:
JS:
function requestData( url )
{
document.getElementById( "spinner" ).style.visibility = "visible";
var xhttp;
xhttp = new XMLHttpRequest( );
xhttp.onreadystatechange = function( ) {
if ( this.readyState == 4 ) {
if( this.status == 200 )
{
document.getElementById( "spinner" ).style.visibility = "hidden";
console.log( this.responseText );
return this.responseText;
}
else
{
document.getElementById( "spinner" ).style.visibility = "hidden";
document.getElementById( "errorsec" ).innerHTML = ":( An unknown error has happened and your request was canceled. Code: " + this.status;
return false;
}
}
};
xhttp.open( "GET", url, true );
xhttp.send( );
}
function handleSignup( )
{
var username = document.getElementById( "un" ).value; // Get text field values
var password = document.getElementById( "pw" ).value;
var requestObject = requestData( "../../FRAMEWORK/createaccount.php?name=" + username + "&password=" + password, false );
var returnObject;
if( requestObject != false )
{
requestObject = JSON.parse( requestObject );
console.log( returnObject.value );
}
}
PHP:
<?php
# REV 1.0.0
$name = $_GET[ "name" ]; # Get values to set
$password = $_GET[ "password" ];
$file = fopen( "../USER_STORE/userindex.json", "r" ); # Load user index
$accounts = json_decode( fread($file, filesize( "../USER_STORE/userindex.json" ) ), true );
fclose($file);
$alreadyExists = false; # Check to see if username already is in use
foreach($accounts as $val) {
if( $val == $name )
{
$alreadyExists = true;
}
}
if( !$alreadyExists ) # If username is not in use
{
$UUID = sizeof( $accounts ) + 1;
$toAppend = array( "username" => $name, "password" => hash( "sha256", $password ) ); # Create append list
mkdir( "../USER_STORE/" . $UUID ); # Append user data
$file = fopen( "../USER_STORE/" . $UUID . "/accountinfo.json", "w" );
fwrite( $file, json_encode( $toAppend ) );
fclose( $file );
$accounts[ $UUID ] = $name; # Create new user index
$file = fopen( "../USER_STORE/userindex.json", "w" ); # Update userindex
fwrite( $file, json_encode( $accounts ) );
fclose( $file );
$return = array( "value" => "created" ); # Return message
echo( json_encode( $return ) );
}
else # Account is in use
{
$return = array( "value" => "account_in_use" ); # Return error message
echo( json_encode( $return ) );
}
?>
XMLHttpRequest
异步工作。所以你的函数 requestData
在 xhttp.send();
之后结束并且没有 return 任何东西。 readystatechange
的事件处理程序(稍后调用,一旦 XMLHttpRequest
实际收到来自服务器的响应)执行 return 一个值,但什么也没做。
解决方案:在该事件处理程序中移动响应的解析和解释。
补充说明:
构建URL时,需要对参数进行转义(使用
encodeURIComponent
)。如果这两个字段中的任何一个包含空格或特殊字符,例如%
或&
,您认为会发生什么情况?当你对密码进行哈希处理时,你必须先对其进行加盐处理。或者使用现成的函数,例如
password_hash
,它将为您完成。您可能想使用
file_get_contents
/file_put_contents
到 read/write 您的用户文件,这将大大简化您的代码。或者更好的是,一个数据库。