jQuery调用php脚本onload
我试图使用jQuery的ajax调用一个php脚本onload。这个脚本将从url web服务返回xml,对其进行解析,然后在页面加载(onload)时将它的一部分显示到div标记中。谈到使用表单,我对php没什么意见,但让php脚本在onload上运行对我来说并不管用。有人能看一下我的密码并给我你的建议吗?提前谢谢 HTML:jQuery调用php脚本onload,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我试图使用jQuery的ajax调用一个php脚本onload。这个脚本将从url web服务返回xml,对其进行解析,然后在页面加载(onload)时将它的一部分显示到div标记中。谈到使用表单,我对php没什么意见,但让php脚本在onload上运行对我来说并不管用。有人能看一下我的密码并给我你的建议吗?提前谢谢 HTML: <!doctype html> <html> <head> <title>Word of the Day&
<!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>
$(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);
});
});
正如您所做的那样,您试图创建一个处理程序,但该处理程序被触发,而该处理程序永远不会工作
编辑:
<!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>
$(document).ready(function() {
$(window).load(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);
});
是否触发了完成或失败事件?如果我将“this.responseText”替换为successful或failure之类的字符串,则会触发“done”事件,但输入“this.responseText”会生成未定义的。word_day.php是否存在于web根目录中?是。所有这些文件都存在并放置在同一文件夹中。您是否在浏览器调试器中放置了断点,以检查哪些变量在.done和.fail中都可以访问?我尝试了此操作,但仍然没有定义。我猜我不应该用这个。responseText。太棒了!成功了。你能帮我理解为什么我必须添加这些参数才能让它工作吗?jQuery就是这样工作的。如果成功,它会将接收到的数据传递给提供的函数(您可以随意调用参数)。
fail
也是如此。它希望您的函数参数接收到它所拥有的值,而您没有这些值,并且试图通过this.responseText
访问它们,您发现这是无效的。请在网站上阅读