JSON 不拉数据?

发布于 2025-01-05 19:21:02 字数 1446 浏览 0 评论 0原文

谁能告诉我为什么这没有提取我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲娇萝莉攻 2025-01-12 19:21:02

如果没有其他信息,很难说清楚,但是:

在调用 .send() 之前设置 ajax.onreadystatechange 否则,理论上,响应可能会在您告诉实例如何处理它之前到达 - 它将完全依赖于浏览器实现。

其次,您是托管它,还是从文件系统本地尝试它?在许多浏览器中,Ajax 调用无法从文件系统运行(它被认为是跨站点脚本漏洞) - 因此最好在本地安装一个简单的 Web 服务器进行测试 - 并从中获取 json。

最后,不要使用 eval,它是邪恶的;-)

改用它

var result = JSON.parse(string);

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

var result = JSON.parse(string);

instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文