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

时间:2016-10-04 20:15:31

标签: javascript php json decode encode

所以我遇到一个问题,即使用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 ) );
}
?>

1 个答案:

答案 0 :(得分:0)

XMLHttpRequest异步工作。因此,您的函数requestDataxhttp.send();之后结束,并且不返回任何内容。 readystatechange的事件处理程序(稍后调用,XMLHttpRequest实际上从服务器接收响应)确实返回一个值,但没有任何操作。

解决方案:在该事件处理程序中移动解析和解释响应。

附加说明:

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

  2. 当您对密码进行哈希处理时,您必须先将其加密。或者使用现成的功能,例如password_hash,它将为您完成。

  3. 您可能希望使用file_get_contents / file_put_contents来读取/写入您的用户文件,这将简化您的代码。或者更好的是,数据库。