Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to use a <code>jsonp</code> request, because the Guardian API is blocking cross-domain requests. Use the JQuery <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow"><code>.ajax</code></a> with <code>dataType: jsonp</code>:</p> <pre><code>$.ajax({ url: footballURL, dataType: 'jsonp', success: function( thisData ) { var data = thisData; // etc ... } }); </code></pre> <p>Your DOM-generation Javascript is a bit of a mess ... but it's easy to get lost writing that kind of code. I'd strongly recommend using some kind of micro-templating engine to handle the data-to-HTML conversion stuff.</p> <hr/> <p><a href="http://jsfiddle.net/UTBd6/5/" rel="nofollow">Here's an example</a> of how to do this using <a href="https://github.com/janl/mustache.js" rel="nofollow">Mustache.js</a>.</p> <pre><code>// create HTML template with data tags var template = "&lt;ul&gt;{{#results}}&lt;li&gt;&lt;ul&gt;&lt;li&gt;&lt;a href='{{webUrl}}'&gt;{{webTitle}}&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;{{/results}}"; // render output var output = Mustache.render(template, thisData.response); // add to the DOM $("#results").html(output); </code></pre> <hr/> <p><a href="http://jsfiddle.net/6rc6a/1/" rel="nofollow">Here's the same example</a> using <a href="http://documentcloud.github.com/underscore/" rel="nofollow">Underscore.js</a>. Same idea, but different implementation that lets you write the template as markup:</p> <pre><code>&lt;script type='text/template' id='article-template'&gt; &lt;% _.each(results, function(article) { %&gt; &lt;ul&gt; &lt;li style="border-bottom: 1px solid #000; width: 80%;"&gt; &lt;a href='&lt;%= article.webUrl %&gt;'&gt;&lt;%= article.webTitle %&gt;&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;% }); %&gt; &lt;/script&gt; </code></pre> <p>​ And the script to render:</p> <pre><code>var template = _.template($("#article-template").html()); var output = template(thisData.response); $("#results").html(output); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload