【发布时间】:2013-07-23 01:36:29
【问题描述】:
在我的 js 页面中,我想使用 ajax() 从 php 页面获取一些变量; 这都是由 html 页面加载触发的。我尝试同时使用 GET 和 POST,但没有任何警报或日志记录到我的控制台,甚至没有错误。
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? -
更好的是,完全删除
data,因为您似乎没有在 PHP 文件中使用它 -
sample.php 真的有输出吗?尝试在网络浏览器中自行访问 sample.php。如果您什么也没看到或内部服务器错误,那么这不是您的 ajax 调用。
-
然后检查 Chrome 控制台或 Firebug 会发生什么..
标签: php jquery post get xmlhttprequest