[英]Content is not loaded into div using javascript
How come the content form the "body.html" file isn't loaded into the "section2"-div?为什么“body.html”文件的内容没有加载到“section2”-div中?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<script src="jquery-1.6.2.min.js"></script>
<script language='javascript' type='text/javascript'>
$(document).ready(function(){
alert("Document ready");
$.get("body.html", function(data){
alert("Data Loaded: ");
$("#section2").html(data);
});
});
</script>
</head>
<body>
<div id="section2">
</div>
</body>
</html>
body.html:身体.html:
Content for all!
Both alerts are triggered.两个警报都被触发。
Thank you in advance!先感谢您!
i'd use load() instead我会改用 load()
$("#section2").load("body.html")
EDIT - i'v seen that $.get works also with two parametrs so i just leave the load() advice编辑 - 我已经看到 $.get 也适用于两个参数,所以我只留下 load() 建议
The problem is that the response headers content type has to be "text/html" when calling body.html.问题是调用body.html时响应头内容类型必须是“text/html”。
To avoid errors, use:为避免错误,请使用:
$.ajax({
url:"body.html",
dataType: "text/html",
success: function(data){
$("#section2").html(data);
}
});
or或者
$.get("body.html",function(data){
$("#section2").html(data);
}
, "text/html");
or或者
$("#section2").load("body.html");
Check out the jQuery.get documentation.查看jQuery.get文档。 It looks like jQuery is trying to help you out by sending the data back as a JS XML document, when you really want HTML.
看起来 jQuery 试图通过将数据作为 JS XML 文档发送回来帮助您,而您确实需要 HTML。 Try
$.get(<url>, null, <successFunc>, "html")
(see the documentation on the $.ajax
method for more on those parameters)尝试
$.get(<url>, null, <successFunc>, "html")
(有关这些参数的更多信息,请参阅$.ajax
方法的文档)
This worked for me...这对我有用...
All I did was link to the external jquery and changed what file I was calling...我所做的只是链接到外部 jquery 并更改了我正在调用的文件...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script language='javascript' type='text/javascript'>
$(document).ready(function(){
alert("Document ready");
$.get("serverTime.php", function(data){
alert("Data Loaded: ");
$("#section2").html(data);
});
});
</script>
</head>
<body>
<div id="section2">
</div>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.