PHP json编码在javascript中没有正确解码

PHP json encode not properly decoding in javascript

本文关键字:解码 json 编码 javascript PHP      更新时间:2023-09-26

所以我有一个问题,使用php编码的数据在javascript中不能正确解码。错误如下:误差

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异步工作。所以你的函数requestDataxhttp.send();之后结束,不返回任何东西。readystatechange的事件处理程序(一旦XMLHttpRequest实际接收到来自服务器的响应,稍后调用该事件处理程序)确实返回一个值,但对它不做任何处理。

解决方案:将响应的解析和解释移到事件处理程序中。

其他说明:

  1. 当您构建URL时,您需要转义(使用encodeURIComponent)参数。如果两个字段中的任何一个包含空格或特殊字符,如%&,您认为会发生什么?

  2. 当您散列密码时,您必须首先对其进行盐处理。或者使用一个现成的函数,比如password_hash,它会为你做这件事。

  3. 您可能希望使用file_get_contents/file_put_contents来读写您的用户文件,这将简化您的代码相当多。或者最好是一个数据库。