I am trying to use jQuery ajax to call PHP and return JSON.
My test is very simple, but all I seem to be getting is an empty object.
I am not getting any PHP errors in the LOG File.
jqXHR is shown as an object with the 'alert', but does not appear in the 'Chrome' console.
The 'Unpacked' alert does not show at all
I have obviously missed something or made some stupid error, but I can not see what I have done wrong.
HTML
<body>
<form id="frmSearch" onsubmit="matchQryString();">
Search: <input type="text" name="qryWords" value="" />
<input type="submit" name="submit" value="Search" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
function matchQryString() {
alert("Running AJAX");
$.ajax({
type:'POST',
url: 'rtnJSON.php',
data:$('#frmSearch').serialize(),
dataType: 'json',
success: function(response) {
alert("PHP Return SUCCESS");
alert(response);
},
error: function (jqXHR, exception) {
alert("PHP Return ERROR");
alert(jqXHR);
alert(exception);
console.log(jqXHR);
var op = "";
for (property in jqXHR) {
op += property + ': ' + obj[property]+'; ';
}
alert ("Unpacked: " + op);
}
});
return false ;
}
</script>
</body>
PHP
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
header('Content-type:application/json;charset=utf-8');
echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5}
?>