我正在尝试使用jQuery的ajax在加载时调用php脚本。该脚本将从urlweb服务返回xml,对其进行解析,然后在页面加载(onload)时将其片段显示到div标记中。在使用表单时,我可以使用php,但让php脚本在加载时运行对我来说不起作用。有人能看看我的代码并给我你的建议吗?提前谢谢。
HTML:
<!doctype html>
<html>
<head>
<title>Word of the Day</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="code.js"></script>
</head>
<body>
<h3>Word of the Day: </h3>
<div id="word_day"></div>
</body>
</html>
JavaScript:
$(document).ready(function() {
$(window).load(function() {
$.ajax({
post: "GET",
url: "word_day.php"
}).done(function() {
alert(this.responseText);
}).fail(function() {
alert(this.responseText);
});
});
});
我没有添加我的PHP代码,因为我很确定这不是我沮丧的原因。
您不需要这两个处理程序,只需要一个:
$(document).ready(function()
{
$.ajax(
{
post: "GET",
url: "word_day.php"
}).done(function()
{
alert(this.responseText);
}).fail(function()
{
alert(this.responseText);
});
});
正如你所做的那样,当处理程序启动时,你正试图创建一个处理程序,但这永远不会起作用。
编辑:
你完成/失败的部分应该看起来像:
}).done(function(data)
{
alert(data);
}).fail(function(jqXHR, textStatus, errorThrown)
{
alert(textStatus);
});