Can you run an ajax request inside of a click function? I have been working with an api and I need to do a second request to get the comments for an image/video that was clicked. I am really new with ajax request and api’s. And I have been working on a small script piecing things together.
First off I know I am not accessing the url correctly for the second ajax request. I dont know how to re-use the code for that inside of this click event (if thats even where it goes). Second I dont know the proper way to access the array of comments. You can see in my butchered up code below I am lost for this part of the functionality.
My console error message is this - "GET http://localhost/ig/insta.apiUrl 404 (Not Found) "
I know I have things wrong please bare with me.
(function () {
"use strict";
var insta = window.insta = window.insta || {}; // insta Object
insta = {
apiUrl: 'https://api.instagram.com/v1/users/self/feed?access_token=[token here]&callback=?',
init: function () {
$.ajax({
type: 'GET',
url: insta.apiUrl,
dataType: 'json',
success: insta.success
});
},
renderGrams: function (grams) {
$.each(grams, function(index, gram) {
var gramHtml = '<div class="col-md-3"><p><img class="img-circle" style="margin-right: 5px" width="60" src="' + gram.user.profile_picture + '">' + gram.user.username + '</p><a href="#myModal" data-toggle="modal" data-username="' + gram.user.username +'" data-img-url="' + gram.images.standard_resolution.url + '" data-tags="' + gram.tags + '">';
if (gram.type === 'image') { gramHtml = gramHtml + '<img class="img-thumbnail" src="' + gram.images.low_resolution.url + '"/>'; }
if (gram.type === 'video') { gramHtml = gramHtml + '<video class="img-thumbnail" src="' + gram.videos.low_resolution.url + '"/>'; }
gramHtml = gramHtml + '</a></div>';
$('.results').append(gramHtml);
});
},
setupClickEvent: function () {
$('a').click(function(){
var largeImage = $(this).data('imgUrl');
var username = $(this).data('username');
var tags = $(this).data('tags');
$('.modal-title').html(username);
$('.modal-body').html('<div class="col-md-6"><img class="img-responsive" src="' + largeImage + '"><div class="tagwrap">' + tags + '</div></div>');
$.getJSON('insta.apiUrl', function(index, gram) {
$('.comments-container').html('<p>' + comments.data.text + '</p>');
});
});
},
success: function (responseData) {
if (responseData.meta.code === 200) {
insta.renderGrams(responseData.data);
insta.setupClickEvent();
} else {
$('.results').html('<h1>An Error Occured</h1><p>' + responseData.meta.error_message + '</p>');
}
}
};
// This actually fires fires everything off
// Follow from here
insta.init();
}());
Here is some of the json for the comments -
"comments": {
"count": 6,
"data": [
{
"created_time": "1391457298",
"text": "First like xd",
"from": {
"username": "lars010999",
"profile_picture": "http:\\/\\/images.ak.instagram.com\\/profiles\\/profile_248947423_75sq_1379584795.jpg",
"id": "248947423",
"full_name": "Lars Lopulalan"
},
"id": "647913236748481826"
},
{
"created_time": "1391457540",
"text": "@blamay48 @bhamilton543 is a for sure character!!\\ud83d\\ude02 #tourguide",
"from": {
"username": "mepmx",
"profile_picture": "http:\\/\\/images.ak.instagram.com\\/profiles\\/profile_340656728_75sq_1382493493.jpg",
"id": "340656728",
"full_name": "MEPMX\\ud83d\\udcf7@mepmx"
},
"id": "647915264509916589"
},