2016-10-16 12 views
0

私は、より大きなプロジェクトを簡単に実行できるようにプロトタイプの使用を実践してきました。私はプロセスを分けることに焦点を当てていますが、私はスコープの問題であると考えているようです。 MYSQLからJSONPHPページに情報を送信すると、機能createAccountConfirmが存在しないというエラーが表示されます。 JSONリクエストはレスポンスが関数を実行するのを待っていますが、その関数はアクセス可能ではないようです。誰かが私が間違っているところで光を放つことができるので、このプロジェクトで軌道に乗ることができます。前もって感謝します!Javascriptプロトタイプの関数スコープ

クライアント側のコード:

_wolc.prototype.account = { 

createAccount: function() { 

    var subArr = []; 

    subArr[0] = document.getElementById("newName").value.toString(); 
    subArr[1] = document.getElementById("newPassword_1").value.toString(); 
    subArr[2] = document.getElementById("newPassword_2").value.toString(); 
    subArr[3] = document.getElementById("newEmail").value.toString(); 

    if (subArr[1] === subArr[2]) { 

     // Pass the values to the server side function checkUser for validation; return to createAccountConfirm() 
     if (window.XMLHttpRequest) { 
      // code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp = new XMLHttpRequest(); 
     } else { 
      // code for IE6, IE5 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 

     xmlhttp.onreadystatechange = function() { 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 

       this.createAccountConfirm(JSON.parse(xmlhttp.responseText)); 

      } 
     }; 

     xmlhttp.open("GET","create_account.php?un="+subArr[0]+ 
              "&pw="+subArr[1]+ 
              "&em="+subArr[3] 
              ,true); 
     xmlhttp.send(); 

    } else { 

     console.log("Passwords don't match"); 

    } 

}, 

createAccountConfirm: function (response) { 

    console.log(response); 

} 

}; 

Serverコード:

<?php 

include 'connection_settings.php'; 

$usr = $_GET['un']; 
$psw = $_GET['pw']; 
$eml = $_GET['em']; 
$tms = date ("Y-m-d H:i:s"); 

$psw = md5($psw); 

$con = mysqli_connect($localhost,$username,$password,$database); 

// Check connection 
if (!$con) { 

    die('Could not connect: ' . mysqli_error($con)); 

} 

mysqli_select_db($con,$database); 

$sql="INSERT INTO user_accounts (Account_Name, Join_Date, Last_Date,Account_Password,Account_Email) 
VALUES ('".$usr."', '".$tms."', '".$tms."','".$psw."','".$eml."')"; 

if ($con->query($sql) === TRUE) { 

    echo json_encode("New record created successfully"); 

} else { 

    echo json_encode("Error: New record failed to create"); 

} 

mysqli_close($con); 

?> 

答えて

1

詳細な説明のためHow to access the correct `this` context inside a callback?を参照してくださいが、基本的thisネストされた(コールバック)関数では、あなたの_wolc.prototype.accountオブジェクトを参照していません。

_wolc.prototype.account = {  
    createAccount: function() { 
    // [...] 

    // save the reference to `this` to use in the callback: 
    var self = this; 

    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 

     // use `self` instead of `this` in the callback: 
     self.createAccountConfirm(JSON.parse(xmlhttp.responseText)); 
+0

オブジェクト 'wolc.account'を作成関数に渡して動作させています。私はこれまでに試したことがあったが、オブジェクトを間違って入れたにちがいない。ありがとう!私はこれを読み上げるつもりですが、問題を修正しました。もちろん、おそらくそれを渡す必要はありませんが、むしろJSON部分を呼び出す前に 'self'を宣言する変数を作成してください。 – xxSithRagexx

関連する問題