In my js page I want to get some variables from the php page using ajax(); This is all triggered by the html page load. I've tried to use both GET and POST, but nothing alerts or logs to my console like it supposed to, not even the error.
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="http://XXXXXX/bn/sample.js"></script>
</head>
<body>
<div>Welcome! </div>
</body>
</html>
JS: (sample.js)
$(function(){
$.ajax({
url : "http://XXXXXX/bn/sample.php",
type : 'GET',
data : data,
dataType : 'json',
success : function (result) {
alert(result.ajax); // "Hello world!" should be alerted
console.log(result.advert);
},
error : function () {
alert("error");
}
});
});
PHP:
<?php
$advert = array(
'ajax' => 'Hello world!',
'advert' => 'Working',
);
echo json_encode($advert);
?>