My Code so far looks like:
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://content.guardianapis.com/search?q=world%20&api-key=4dftzq5rzqbkc7h3b3epsgem",
dataType: "JSON",
cache: false,
success: function (data){
console.log(data);
}
});
});
I do not know now how to call the actual content from the api. The JSON looks like:
You're calling to the api, when the document is ready.
You can fill a list with the current results from the response, by accessing data.response.results
.
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://content.guardianapis.com/search?q=world%20&api-key=4dftzq5rzqbkc7h3b3epsgem",
dataType: "json",
cache: false,
success: function(data) {
console.log(data);
for (var i = 0; i < data.response.results.length; i++) {
$("#results").append("<li>" + data.response.results[i].webTitle + "</li>");
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="results">
</ul>
</div>
When you specify dataType: "json"
it evaluates the response as JSON and returns a JavaScript object. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown.
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://content.guardianapis.com/search?q=world%20&api-key=4dftzq5rzqbkc7h3b3epsgem",
dataType: "JSON",
cache: false,
success: function(data) {
console.log(data); //already parsed object
console.log(data.response); //try accessing your property.
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</div>
The question is, what do want to archive?, data should be an object that already contains everything you need. You could now do something like: data.response.results[0].webTitle