简体   繁体   English

有人可以帮我理解 AJAX (JSON) 问题吗?

[英]Can someone help me understand AJAX (JSON) problem?

My goal is to create an personal application out of my ActionScript3 video player.我的目标是用我的 ActionScript3 视频播放器创建一个个人应用程序。 I want to be able to hold keep the single.swf file in my browser and through AJAX, pass the.swf the url to my videos.我希望能够将 single.swf 文件保留在我的浏览器中,并通过 AJAX 将 url 的.swf 传递给我的视频。 I've chosen to use JSON as my data.我选择使用 JSON 作为我的数据。

I'm new to JSON and I've hit a wall.我是 JSON 的新手,我碰壁了。 It seems completely easy, but at first I wasn't even able to even get my locally hosted.json file into my webapp.这似乎很容易,但起初我什至无法将本地托管的.json 文件放入我的 web 应用程序中。 It was working when I tried to do this with XML.当我尝试使用 XML 执行此操作时,它正在工作。 After a bunch of trouble shooting, it is now in fact getting my XMLHttpRequest.经过一堆故障排除,现在它实际上得到了我的 XMLHttpRequest。

I'm trying to keep with backwards compatibility as much as possible, and thus I'm using the json2.js library to secure that notion.我试图尽可能地保持向后兼容性,因此我使用 json2.js 库来确保这一概念。

My current issue is not being able to parse my XMLHTTPRequest.我当前的问题是无法解析我的 XMLHTTPRequest。 To be honest, I'm not even sure if what I'm doing is right as everywhere I look for examples, they're almost all pointing to a solution that writes the JSON into the actual webpage.老实说,我什至不确定我所做的是否正确,因为我到处寻找示例,它们几乎都指向将 JSON 写入实际网页的解决方案。

My external JSON file: test.json.我的外部 JSON 文件:test.json。

{ "video":"test.f4v", "thumb":"test.jpg", "title":"The test", "caption":"TEST TEST TEST TEST TEST", "minutes":01, "seconds":43 }

I'm positive the JSON file is valid.我很肯定 JSON 文件是有效的。

Here is my html/javascript:这是我的 html/javascript:

<script type="text/javascript" src="js/json2.js"></script>
    <script type="text/javascript">
    window.onload = initAll();

    function initAll(){
        var jsonData = {};
        var xhr = false;

        if(window.XMLHttpRequest){ //Chrome, Firefox, Opera, Safari, IE7+
            xhr = new XMLHttpRequest();
        } else if(window.ActiveXObject){ //IE5 + IE6
            try{
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e){
                try{
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    alert("Could not make XMLHttpRequest");
                }
            }
        }

        if(xhr){
            xhr.open("GET", "js/ajax/test.json", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200){
                    try{
                        jsonData = JSON.parse(xhr.responseText);
                        alert(jsonData.title);
                        document.getElementById("vidtitle").innerHTML = xhr.responseText.title;
                        document.getElementById("vidcaption").innerHTML = jsonData.caption;
                    } catch(e){
                        alert("Unable to parse jsonData.");
                    }
                }
            };
            xhr.send(null);
        }
    }
</script>   
</head>
<body><div class="vidcontent">
<h2 id="vidtitle"></h2>
<p id="vidcaption"></p>

I'm doing this locally on my server, but I have uploaded the files to my web host and still get the same issues.我在我的服务器上本地执行此操作,但我已将文件上传到我的 web 主机,但仍然遇到相同的问题。

Firebug tells me it has the data and I can even read it through the console. Firebug 告诉我它有数据,我什至可以通过控制台读取它。 Now, The code works in Firefox, but not Chrome or IE8 (IE8 Works occasionally when I put it into compatibility mode, but not always <.< ) I can't test on Safari or Opera right now.现在,该代码在 Firefox 中有效,但在 Chrome 或 IE8 中无效(当我将其置于兼容模式时,IE8 偶尔会工作,但并非总是 <.< )我现在无法在 Safari 或 Opera 上进行测试。

Is there any way I can get this working in these browsers?有什么方法可以让我在这些浏览器中工作吗? I have attempted $.parseJSON in the JQuery library, but I was having issues with that as well.我在 JQuery 库中尝试了 $.parseJSON,但我也遇到了问题。 I am interested in JQuery as well if anyone can explain the solution with it.如果有人可以用它解释解决方案,我也对 JQuery 感兴趣。

Your JSON is invalid: http://www.jsonlint.com/您的 JSON 无效: http://www.jsonlint.com/

Leading 0's are not allowed in numbers apparently.显然,数字中不允许使用前导 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM