开发者

Parsing Valid JSON with AJAX?

Hi I am still learning this JSON stuff...

I think I found a valid JSON:

http://www.nfl.com/liveupdate/scorestrip/ss.json

Problem is I am not sure how I can parse this using AJAX?

Is this possible to do? I have also heard of MooTools what is the difference?

Also here is some code I have just to play around but doesn't seem to work:

$.ajax({
    type: "GET",
    url: "http://www.nfl.com/liveupdate/scorestrip/s开发者_开发技巧s.json",
    dataType: "json",
    success: function(data) {
        // Interpret response
       for (var i = 0; i < data.gms.length; i++) {
        document.write("Day: " + data.gms[i][0]);
        document.write("<br/>");
        document.write("Time: " + data.gms[i][1]);
        document.write("<br/><br/>");
    }
    }
});


JSON stands for "JavaScript Object Notation". AJAX stands for "Asynchronous Javascript And XML". MooTools is a crossbrowser javascript framework which provides developers an object oriented workflow with javascript.

Don't confuse apples with peas. All three technologies are often used together, but are different things. JSON is a notation to describe javascript objects, arrays and literals; AJAX is for requesting and retrieving documents (including documents in JSON format) and MooTools can be used to make AJAX calls (probably retrieving a document in JSON format – or an XML fragment).

JSON is pure javascript, there is no need to "parse" it in any way, the javascript interpreter will do that for you. AJAX is often used to describe XMLHttpRequests: http requests originating from javascript, often retrieving JSON or XML/HTML data. MooTools allows you with relatively little code to make use of the XMLHttpRequest feature trough its Request class:

var request = new Request.JSON({
  url: 'http://www.nfl.com/liveupdate/scorestrip/ss.json',

  onRequest: function(){
    alert('We are loading now ...'); // abuse alert(), quite annoying and so 1990
  },

  onComplete: function(jsonObj) {
    // jsonObj is the retrieved JSON. Access it like you would normal javascript objects and arrays
  }

}).send();


I am guessing your problem is cos your making a cross domain request and your browser is blocking that requst. To process a json response from another domain you need to use jsonp datatype. Its basically chaging the dataType:"jsonp" in your ajax request.

Under the same origin policy, a web page served from server1.example.com cannot normally connect to or communicate with a server other than server1.example.com. An exception is the HTML element. Taking advantage of the open policy for elements, some pages use them to retrieve Javascript code that operates on dynamically-generated JSON-formatted data from other origins. This usage pattern is known as JSONP.

Do read about it. For more info abt json in jquery ajax: http://api.jquery.com/jQuery.ajax/

your updated code:

$.ajax({
    type: "GET",
    url: "http://www.nfl.com/liveupdate/scorestrip/ss.json",
    dataType: "jsonp",
    success: function(data) {
        // Interpret response
       for (var i = 0; i < data.gms.length; i++) {
        document.write("Day: " + data.gms[i][0]);
        document.write("<br/>");
        document.write("Time: " + data.gms[i][1]);
        document.write("<br/><br/>");
    }
    }
});

since the site doesnt seem to be under your control you can have the script hit your url i.e your local site url wherein your local server now contacts the actual url gets the json and sends it as it is


I would recommend you first study the http requrest, Whats going on when you request data or receive data. Once you learn the basic, then this will be easy to request and receive data.

HTTP Request

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜