简体   繁体   中英

Ajax url encoding issue

What i am attempting to do is send an encrypted message and the corresponding key (two way encryption) to a php page for it to be decrypted and then have the result returned in the response.

Here is an example of the encrypted message i am trying to send with jquery.

var message = 'oPnHK7DE33xOLZok/23a92XH9NI3SlHGCulnh6+IuZN4cGhymYm5yxOmDynCDAG8u+cAbJ4KifxzsWsGgmTXoZoAtjkAhph/eWyuwMNfviNtgmz4x02JVJ6Rc6wDsqzzd6Mrl88ZZXyEshD1/+9JRS9rNalCtv//pC2FRAZMQhH5wxDn9kb6JITSs/aagUGFbLmq+jxg5ty55SKmri6IJg==';

var key = 'password';

$.post('decodeMessage.php?message=' + encodeURIComponent(message) + '&key=' + key, function(data) {
    // do stuff with returned data here
});

The receiving php code is as follows

 <?php
 $encrypted = rawurldecode($_POST['message']);
 $key = $_POST['key'];

 $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

 echo $decrypted

 ?>

The problem that i have is that it is just returning gibberish and not actually decoding the message.

I have tested this without ajax by setting the value of $encrypted to the encoded value that is being passed in the ajax request and it works fine.

i would appreciate it if anyone could offer me any guidance as to why this is occurring.

Many thanks.

Send the data as an object instead.

$.post('decodeMessage.php', {message: message, key: key}, function(data) {
    // Wohoo
});

You see, jQuery takes care of this stuff for you, under the layers of obscure code :-)

Try it this way

$.post("decodeMessage.php", { message: message, key: key },function(data){
   // ....
});

your passing a query string

$.post('decodeMessage.php?message=' + encodeURIComponent(message) + '&key=' + key, function(data) {

then use $_GET instead of post

 $encrypted = rawurldecode($_GET['message']);
 $key = $_GET['key'];

if you want to handle the elements as a POST variables in PHP change the jquery code to read as follows

var data = { message: message, key: key };
$.post('decodeMessage.php', data, function(data) {
   //handler 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM