2016-10-09 7 views
2

JSON.parse()私のコードではうまくいかないようですが、何が原因なのか分かりません。JSON予期しないトークンがあります

JSコード:

var obj = JSON.parse(this.responseText); 
console.log(obj.status + " " + obj.type); 
if (obj.status == "success") { 
    document.cookie = "accounttype=" + obj.type; 
    document.cookie = "name=" + obj.name; 
    var accounttype = getCookie(accounttype); 
    if (accounttype == "Seller") { 
     window.location.href = "../html/sell.html"; 
    } 
} 

PHPコード:

$count = mysqli_num_rows($result); 
    if($count == 1){ 
     echo '{"status": "success", "type":"$account", "name":"$name"}'; 
    }else{ 
     echo '{"status": "failed"}'; 
    } 

は、あなたたちは私を助けることを願って、ありがとう!

EDITはCODE

var xmlhttp = new XMLHttpRequest(); 
xmlhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     var str = JSON.stringify(this.responseText); 
     var obj = JSON.parse(str); 
     console.log(obj.status + " " + obj.type); 
     if (obj.status == "success") { 
      document.cookie = "accounttype=" + obj.type; 
      document.cookie = "name=" + obj.name; 
      var accounttype = getCookie(accounttype); 
      if (accounttype == "Seller") { 
       window.location.href = "../html/sell.html"; 
      } 
     } else { 
      sweetAlert("Oops...", "Invalid username or password!", "error"); 
      document.getElementById("password").value = ""; 
     } 
    } 
} 
xmlhttp.open("POST", "../php/login.php", true); 
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
xmlhttp.send("username=" + username + "&" + "password=" + password); 

を更新し、ここで更新PHPコードです:

<?php 
    include("checkdbconnection.php"); 

     session_start(); 


    $username = mysqli_escape_string($conn, $_POST['username']); 
    $password = mysqli_escape_string($conn, $_POST['password']); 
    $password = md5($password); 

    $getLogin = "SELECT * FROM user WHERE username = '$username' and password = '$password'"; 
    $result = mysqli_query($conn, $getLogin); 

    $row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
    $name = $row["firstname"]; 
    $account = $row["account_type"]; 

    if(!$result){ 
     echo "Query Error: ". mysqli_error($conn); 
     die(); 
    } 

    $jsonsuccess = ["status": "success", "type": $account, "name": $name]; 
    $jsonfail = ["status": "failed"]; 
    $jsonsuccstring = json_encode($jsonsuccess); 
    $jsonfailstring = json_encode($jsonfail) 
    $count = mysqli_num_rows($result); 
    if($count == 1){ 
     echo $jsonsuccstring; 
    }else{ 
     echo $jsonfailstring; 
    } 
?> 

コンソールは、両方のJSONは値を戻したためundefinedを返します。

答えて

0

使用json_encode()

あなたのサーバー側の応答は、あなたのJSコードは、あなたがあなたのJSON文字列を確認する必要があり

$.ajax({ 
    type: "GET", 
    url: "URL.php", 
    data: data, 
    success: function(data){ 
     // Here is the tip 
     var data = $.parseJSON(data); 
     //Here is success 
     alert(data.status); 
    } 
}); 

EDIT

ようにする必要があり

$json = ["status": "success", "type": $account, "name": $name]; 

$jsonstring = json_encode($json); 
echo $jsonstring; 
die(); 

でなければなりません。 OKであれば、これを試してみてください:

var jsonStr="your json string"; 
var json=JSON.stringify(jsonStr); 
json=JSON.parse(json) 
+0

エラーは例外:予期しないトークン<に変更されました。それが何を意味するのか? –

+0

あなたの更新されたコードを教えてください? –

+0

もう一度、あなたの編集を遅く読んでください。今すぐ試してみてください。 –

関連する問題