SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to read JSON from an API with jQuery?

    I'm trying to get statistics from sendgrid by using getJSON(). Their JSON feed reads:

    Code:
    [{"date":"2009-06-20",
    "requests":12342,
    "bounces":12,
    "clicks":10223,
    "opens":9992,
    "spamreports":5,
    "unique_clicks":3,
    "unique_opens":6,
    "blocked":7},
    
    {"date":"2009-06-21",
    "requests":32342,
    "bounces":10,
    "clicks":14323,
    "opens":10995,
    "spamreports":7,
    "unique_clicks":3,
    "unique_opens":9,
    "blocked":4},
    
    {"date":"2009-06-22",
    "requests":52342,
    "bounces":11,
    "clicks":19223,
    "opens":12992,
    "spamreports":2,
    "unique_clicks":5,
    "unique_opens":2,
    "blocked":8}]
    This is the jquery code to render it into the browser, but nothing is showing up?

    Code:
    $(document).ready(function(){
    var stats =
    "https://sendgrid.com/api/stats.get.jsonapi_user=youremail@domain.com&api_key=secureSecret&days=2"
    	
    	$.getJSON(stats, function(key, value) {
    		$('<ul>', {
    		    id : 'sg_stats',
    		    html : '<li> ' + key + '</li><li>' + value '</li>'
    		})
    	});	
    });
    Sendgrid is the feed I'm trying to access, should I put wrap this in an ajax call instead?
    Isn't this a type of AJAX call anyways?
    Am I missing something else in my code?

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Please have a read of the jQuery documentation as your currently using your anonymous function incorrectly.

    http://api.jquery.com/jQuery.getJSON/
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $.getJSON("test.js", function(json) {
        $.
    each(json, function(){
            
    alert(this.date);
        });
     }); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •