I am trying to do an ajax 'post' call only once when a page loads. My code looks like this :
<script type='text/javascript'>
$.post("amazon/apicalls/ConfirmAndAuthorize.php",{}).done(function(data) {
var obj = jQuery.parseJSON(data);
$.each(obj, function(key, value) {
if (key == 'confirm') {
var str = JSON.stringify(value, null, 2);
$("#confirm").html(str);
} else if (key == 'authorize') {
var str = JSON.stringify(value, null, 2);
alert(str);
$("#authorize").html(str);
}
});
});
</script>
I don't see anything wrong with the way I'm doing it however I am getting an error when the page loads : Cannot read property 'post' of undefined
.
What am i forgetting here ?
EDIT:
The answer was a mix between Artem Gorlachev's and Milan Chheda's comments. It appears JQuery was only included in the page footer. So all that was needed was to wait for the page to load completely.
I achieve that without using JQuery with the methods presented in this answer to an other question.
Here is what it looks like :
<script type='text/javascript'>
document.addEventListener("DOMContentLoaded", function(event) {
$.post("amazon/apicalls/ConfirmAndAuthorize.php",{}).done(function(data) {
...
});
});
</script>