user1953825
user1953825

Reputation:

Having trouble with my JSON HTTP request

PREFACE: I'm new to JavaScript and working through some tutorials, and I'm stuck here!

I'm running the following locally on WAMP with PHP 5.3.13 installed. I've received no errors in the console, but for some reason nothing is displayed!

Anyone have any idea why?

Minimized JSON:

{"channel":{"title":"RSS Sample","description":"A sample RSS Feed","link":"http://www.website.com","copyright":"Copyright 2012"}}

JavaScript:

var xhr = new XMLHttpRequest();

xhr.open("GET", "rss.json", true);

xhr.onreadystatechange = function() {
if (xhr.readystate === 4) {
    var status = xhr.status;

    if ((status >= 200 && status < 300) || status === 304) {
        var rss = JSON.parse(xhr.responseText);

        alert(rss.channel.description);
    } else {
        alert("Request unsuccessful");
    }
}
};

xhr.send(null);

Any help would be greatly appreciated! :)

Upvotes: 1

Views: 4028

Answers (2)

Phil
Phil

Reputation: 164776

The correct property name is readyState (capital "S").

Upvotes: 2

Ivan
Ivan

Reputation: 429

I haven't seen using JSON this way, though I'm more a php guy.

Try get that JSON using AJAX. It would look like this

$("#a_button").click(function() {
        $.ajax({
               type: "POST",
               url: "json.php",
               dataType: "json",
               cache: false,
               success: function(data)
               {
                    alert(data.smth);
               }
             });
        return false;
    });

A always use this way and it works just fine.

Upvotes: -1

Related Questions