在我的js页面中,我想使用ajax()从php页面获取一些变量; 这全部由html页面加载触发。我试图同时使用GET和POST,但没有任何警报或日志到我的控制台,就像它应该,甚至没有错误。为什么不是我的AJAX获取请求工作
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);
?>
也许你还没有定义'data'了吗? – Blazemonger
更重要的是,彻底删除'data'因为你似乎不在你的PHP文件中使用它 –
sample.php实际输出任何事情?尝试在webbrowser中单独使用sample.php。如果你没有看到任何内容或内部服务器错误,那么它不是你的ajax调用。 – Bil1