我想使用 jquery 获取存储在 mysql 中的数据,并将其动态填充到网页上。 我试过这段代码,但它不起作用。
<html>
<head>
<script src="jquery-1.7.2.js"></script>
</head>
<body>
<pre>
<script>
var url = "https://natiweb-natiweb.rhcloud.com/game.php";
$.getJSON(url,function(data){
$.each(data,function(i,user){
alert("inside JSON");
alert(user.appname+"\n"+user.applink);
var a = document.createElement('a');
var linkText = document.createTextNode(user.appname);
a.appendChild(linkText);
a.title = linkText;
a.href = user.applink;
document.body.appendChild(a);
var b = document.createElement('br');
document.body.appendChild(b);
});
}
);
</script>
</pre>
</body>
</html>
它从不执行 getJSON 查询。我不知道怎么了。 “JSON 内部”没有打印出来。我尝试使用专用的 js 文件编写相同的脚本代码并且它有效。我希望它在 body 内部工作,因为我必须通过从 mysql 获取链接来动态创建链接。
最佳答案
首先,在文档准备好后执行您的代码:
$(function(){ /* your code */ });
或
$(document).ready(function(){
/* your code*/
});
- 其次,使用$.ajax,传入错误回调,检查是否有异常。
关于javascript - 如何使用jquery获取数据并动态填充到网页上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16062709/