开发者

Can someone help me understand AJAX (JSON) problem?

My goal is to create an personal application out of my ActionScript3 video player. 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. I've chosen to use JSON as my data.

I'm new to JSON and I've hit a wall. It seems completely easy, but at first I wasn't even able to even get my locally hosted .json file into my webapp. It was working when I tried to do this with XML. After a bunch of trouble shooting, it is now in fact getting my 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 开发者_JAVA百科notion.

My current issue is not being able to parse my 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.

My external JSON file: 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.

Here is my 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.

Firebug tells me it has the data and I can even read it through the console. 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.

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. I am interested in JQuery as well if anyone can explain the solution with it.


Your JSON is invalid: http://www.jsonlint.com/

Leading 0's are not allowed in numbers apparently.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜