I just want to do the simple thing: on click get a variable value from the php file a display it on my page. E.g.
<div class="click" onclick="getMyValue()">Click me</>
<script>
function getMyValue() {
$.ajax({
type: "POST",
url: "basket_count.php",
dataType: "json",
success: function(res) {
console.log(res['item_count']);
console.log("success");
},
error: function() {
console.log("Error");
}
});
}
</script>
basket_count.php
$item_count = 3;
echo json_encode($item_count);
Not working though. I'm getting "Error" in my console.log();
UPD:
I've tried to add this to error: error: function(err) {
console.log(err);}
Below is the message i'm getting:

UPD: Nothing was wrong with the code, the issue was that I was including "header.php" in my php file, so there was some conflict with it. I removed it and it started to work just fine.
getMyValue()codegetMyValuefunction, I cannot seegetMyValuefunction defined in your code.$result['item_count'] = 3; echo json_encode($result);and in your ajaxconsole.log(res.item_count);