2016-10-04 40 views
0

所以我有一個問題,即使用php編碼的數據在javascript中沒有正確解碼。這裏是錯誤: ErrorPHP json編碼在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)); 
} 
?> 
+0

也許你有空間發生某處或某些不可見的字符,可能在頁面頂部的<?php'之前? – Rasclatt

+0

在嘗試JSON.parse()之前requestObject看起來像什麼? – tpdietz

+0

以下是它的樣子:{「returnValue」:「account_in_use」} – steve

回答

0

XMLHttpRequest以異步方式工作。所以你的功能requestDataxhttp.send();之後結束,並且不返回任何東西。 readystatechange的事件處理程序(稍後調用,一旦XMLHttpRequest實際上接收到來自服務器的響應)確實會返回一個值,但不會執行任何操作。

解決方案:移動該事件處理程序中響應的解析和解釋。

其他注意事項:

  1. 當你創建一個URL,你需要轉義(使用encodeURIComponent)的參數。如果兩個字段中的任何一個包含空格或特殊字符(如%&),您認爲會發生什麼?

  2. 當您散列密碼時,您必須先將其加鹽。或者使用現成的功能,如password_hash,這將爲您做。

  3. 您可能希望使用file_get_contents/file_put_contents來讀取/寫入您的用戶文件,這將簡化您的代碼很多。或者更好的是,一個數據庫。

+0

嗨,所以我重新做了我的代碼,但我仍然有同樣的問題。這裏是新的代碼:[Pastebin](http://pastebin.com/Ycx5AQmw) – steve

+0

使用代碼片段或jsFiddle,它允許直接測試。快速檢查顯示一個錯字:'callback(true,this.resoponseText);'。可能會有更多的錯誤。 – jcaron

+0

錯字實際上是在ide中https://gyazo.com/eed189f0dd5270c53c671b1f1165f817很好的接收! – steve