2015-09-02 42 views
0

所以在這個腳本中變量「encrypted」應該通過ajax發送並傳遞到upload.php,但我無法做到這一點。比方說,在阿賈克斯我用一個不同的變量替換「encrypted」變量,它的工作原理。爲什麼不能通過ajax發送變量「encrypted」到upload.php雖然AJAX不能發送Javascript變量

<script src="http://cryptojs.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> 
<script> 
    var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase"); 

    $(document).ready(function() { 
     // variables to be sent to php 
     var formData = { 
      'encrypted_data': encrypted 
     }; 

     // process the form 
     $.ajax({ 
       type: 'POST', // define the type of HTTP verb we want to use (POST for our form) 
       url: 'upload.php', // the url where we want to POST 
       data: formData, // our data object 
       dataType: 'json', // what type of data do we expect back from the server 
       encode: true 
      }) 
      // using the done promise callback 
      .done(function(data) { 

       // log data to the console so we can see 
       console.log(data); 

       // here we will handle errors and validation messages 
      }); 

     // stop the form from submitting the normal way and refreshing the page 
     event.preventDefault(); 

    }); 
</script> 
+0

將您的腳本引用移至aes.js,以便使用您自己的代碼在腳本塊上方。 – Chev

+0

嘗試過,沒有工作 – AgentPigman

+1

在控制檯中獲取任何錯誤? – Chev

回答

1

您的VAR encrypted是一個對象,你應該序列化:

var formData = { 
    encrypted_data: JSON.stringify(encrypted) 
}; 

或發送任何你需要的屬性:

var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase"); 

alert(encrypted.key);  // 74eb593087a982e2a6f5dded54ecd96d1fd0f3d44a58728cdcd40c55227522223 
alert(encrypted.iv);   // 7781157e2629b094f0e3dd48c4d786115 
alert(encrypted.salt);  // 7a25f9132ec6a8b34 
alert(encrypted.ciphertext); // 73e54154a15d1beeb509d9e12f1e462a0 
+0

還是,沒有工作 – AgentPigman

+1

在你的代碼中有其他錯誤,然後Pigman,因爲這個答案有效。 [這裏是一個演示](http://codepen.io/Chevex/pen/epmmoB?editors=001),我從他的'encrypted'對象中選擇他在回答中使用的屬性。 'formData'是一個帶有這些屬性的對象的序列化字符串,消除了循環引用。 – Chev