I have been trying to return an alert of a value in a php function using AJAX. However, the alert returns the entire php script, instead of looking for where 'echo' is used. Here is my ajax call in JS:
function sendEncryptionToServer(thisData) {
$.ajax({
url: 'http://myurl.com/encrypt.php',
type: 'POST',
data: { method : "encryptData", dataInQuestion : "Hello" },
success: function(response){
alert(response);
console.log("SUCCESS");
},
error: function(response) {
alert(response);
console.log("ERROR");
}
});
}
And here is my php file
<?php
//Encrypt
if(isset($_POST['dataInQuestion']) && $_POST['method'] == 'encryptData')
{
$data = json_encode($_POST['dataInQuestion']);
$ciphertext = "fakecipher";
$api_key = "fakekey";
$encryptedData = openssl_encrypt($data, "AES-128-CBC", $ciphertext + $api_key, OPENSSL_RAW_DATA, $ciphertext);
$returnedEncryptedData = base64_encode($encryptedData);
echo json_encode($returnedEncryptedData); //This shows the encrypted string
}
//Decrypt
if(isset($_POST['dataInQuestion']) && $_POST['method'] == 'decryptData')
{
$data = json_encode($_POST['dataInQuestion']);
$ciphertext = "fakecipher";
$api_key = "fakekey";
$decryptedData = openssl_decrypt($encryptedData, "AES-128-CBC", $ciphertext + $api_key, OPENSSL_RAW_DATA, $ciphertext);
echo $decryptedData;
}
?>
What ends up happening is my alert displays my entire php file, from start to finish. I thought it would look into the isset
and echo out the response based on the data object? If it means anything, I am running a local server via node.JS, but the http:/myurl.com/encrypt.php
exists on a real server.