JSON 不拉数据?
谁能告诉我为什么这没有提取我的 JSON 数据。我认为我的语法正确,但页面没有提取数据。有什么想法吗?
<!DOCTYPE html>
<html>
<head>
<title>PhoneGap Ajax Sample</title>
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript">
function appReady(){
var ajax = new XMLHttpRequest();
ajax.open("GET","http://www.lcbcchurch.com/mobileJSON/homeslideshow",true);
ajax.send();
ajax.onreadystatechange=function(){
if(ajax.readyState==4 && (ajax.status==200||ajax.status==0)){
eval('var data = ' + ajax.responseText + ';');
var theResults = data.results;
var theHTML = '';
for(var i=0;i<theResults.length;i++){
theHTML += [
'<div class="avatar"> <img src='+theResults[i].slideshow-image+' />'].join('');
}
document.getElementById('main').innerHTML = theHTML;
}
}
}
document.addEventListener("deviceready", appReady, false);
</script>
</head>
<body>
<div id="main">
</div>
</body>
</html>
Can anyone fill me in as to why this isn't pulling my JSON data. I think I have the syntax correct but the page doesn't pull the data. Any thoughts?
<!DOCTYPE html>
<html>
<head>
<title>PhoneGap Ajax Sample</title>
<script type="text/javascript" src="phonegap.js"></script>
<script type="text/javascript">
function appReady(){
var ajax = new XMLHttpRequest();
ajax.open("GET","http://www.lcbcchurch.com/mobileJSON/homeslideshow",true);
ajax.send();
ajax.onreadystatechange=function(){
if(ajax.readyState==4 && (ajax.status==200||ajax.status==0)){
eval('var data = ' + ajax.responseText + ';');
var theResults = data.results;
var theHTML = '';
for(var i=0;i<theResults.length;i++){
theHTML += [
'<div class="avatar"> <img src='+theResults[i].slideshow-image+' />'].join('');
}
document.getElementById('main').innerHTML = theHTML;
}
}
}
document.addEventListener("deviceready", appReady, false);
</script>
</head>
<body>
<div id="main">
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有其他信息,很难说清楚,但是:
在调用 .send() 之前设置 ajax.onreadystatechange 否则,理论上,响应可能会在您告诉实例如何处理它之前到达 - 它将完全依赖于浏览器实现。
其次,您是托管它,还是从文件系统本地尝试它?在许多浏览器中,Ajax 调用无法从文件系统运行(它被认为是跨站点脚本漏洞) - 因此最好在本地安装一个简单的 Web 服务器进行测试 - 并从中获取 json。
最后,不要使用 eval,它是邪恶的;-)
改用它
。
Hard to tell without additional information, but:
Set ajax.onreadystatechange before you call .send() otherwise the response could, in theory, arrive before you've told the instance how to handle it - it would be entirely dependent on the browser implementation.
Secondly, are you hosting it, or trying it locally from your filesystem? In many browsers, Ajax calls don't work from the filesystem (it is considered a cross site scripting vulnerability) - so its best to install a simple webserver locally to test on - and fetch the json from that too.
Lastly, don't use eval, its evil ;-)
Use
instead.