My AJAX request to pings the server, and the URL but returns an error because it doesnt like the ‘<‘ as the open tag for the php script.
AJAX script.
$.ajax({
url: "data.php",
type: "POST",
data: JSON.stringify({"data":transaction}),
dataType: "json",
success: function(data, textStatus, jqXHR){
alert("success");
currentProcesses -= 1;
$("<span>").html("Ajax Call Complete. Releasing lock for process").appendTo($(body));
},
error: function(jqXHR, textStatus, errorThrown){
alert("error with ajax call "+textStatus+" "+errorThrown);
}
});
php script
<?php
$win = $_POST['data'];
?>
Am I using the wrong flag settings for the AJAX call?
The body of your POST request is encoded as application/json instead of application/x-www-form-urlencoded.
You aren’t doing anything to make the request say that the data is encoded in JSON and you aren’t doing anything in PHP to try to parse JSON.
As a result, PHP fails to populate
$_POST, because the data is in the wrong format.The solution is: Don’t send JSON. Just give the data property an object as its value, and jQuery will serialise it to application/x-www-form-urlencoded data for you.
(Assuming that
transactionis not a complex data type (like an array or object).Additionally, the
bodyvariable isundefined. You should either use a string (to make it a selector:"body") or pass the body element directly:document.body.