It’s hard for me to understand this. It seems that no matter what I try, PHP always finishes returning an empty array. Here is the code of my main file (index.php):
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$(".ajaxlink").click(function() {
callServer();
return false;
});
});
var test = { "testName": "testValue" }
var testJSON = JSON.stringify(test);
function updatePage(data) {
document.getElementById("testDiv").innerHTML = data;
}
function callServer() {
$.ajax({
type: "POST",
url: "ajax/server.php",
data: testJSON,
success: function(data) {
updatePage(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('An Ajax error occured\ntextStatus = ' + textStatus + '\nerrorThrown = ' + errorThrown + '\nstatus = ' + XMLHttpRequest.status);
}
});
}
</script>
<div id="testDiv">Something here</div>
<a href="test1.htm" class="ajaxlink">Link!</a> <br>
This basically launches the callServer () function when you click "Link!" Then it sends the json test data, that is, {"testName": "testValue"} to server.php. Firebug reports that json-data is indeed sent to server.php.
My server.php looks like this:
<?php
print_r($_POST);
?>
This returns the following in testDiv:
Array
(
)
.ajax , , server.php, . (json, jquery) . Apache 2.2 PHP 5.3.1, - ( -). , , "application/x-www-form-urlencoded; charset = UTF-8 ', .
.