Resources for JSON?

@KamilCybulski wow this is so cool, thanks for the link !!

I have this:

var theQuote = '';
var theAuthor = '';

$.ajax({
    url: 'https://andruxnet-random-famous-quotes.p.mashape.com/', // The URL to the API. You can get this in the API page of the API you intend to consume
    type: 'GET', // The HTTP Method, can be GET POST PUT DELETE etc
    data: {}, // Additional parameters here
    dataType: 'json',
  
    success: function(response) { 
       var r = JSON.parse(response);
    theQuote = r.quote;
      theAuthor = r.author;
 },
    error: function(err) { alert(err); },
    beforeSend: function(xhr) {
    xhr.setRequestHeader("X-Mashape-Authorization", "OgGngs3hmYmshg4xvdL6gmKT1Nmkp1nenSpjsn71sOkr8N4qSZ"); // Enter here your Mashape key
    }
});

console.log(theQuote);

And I keep getting this:

VM1719:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse ()
at Object.success (pen.js:9)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at A (jquery.min.js:4)
at XMLHttpRequest. (jquery.min.js:4)

Holy crap! I actually got it to work!

var theQuote = '';
var theAuthor = '';

$.ajax({
    url: 'https://andruxnet-random-famous-quotes.p.mashape.com/', // The URL to the API. You can get this in the API page of the API you intend to consume
    type: 'GET', // The HTTP Method, can be GET POST PUT DELETE etc
    data: {}, // Additional parameters here
    dataType: 'json',
  
    success: function(data) { 
      
    theQuote = data.quote;
      theAuthor = data.author;
      $('#quote-text').text(theQuote);
      $('#quote-author').text(theAuthor);
    },
    error: function(err) { alert(err); },
    beforeSend: function(xhr) {
    xhr.setRequestHeader("X-Mashape-Authorization", "OgGngs3hmYmshg4xvdL6gmKT1Nmkp1nenSpjsn71sOkr8N4qSZ"); // Enter here your Mashape key
    }
});

1 Like

Well Iā€™ve gotten rid of what I didnā€™t understand (:grin:) just to see what we were working with:

$(document).ready(function (){
	
	
	var theQuote = '';
	var theAuthor = '';

$.ajax({
    url: 'https://andruxnet-random-famous-quotes.p.mashape.com/', // The URL to the API. You can get this in the API page of the API you intend to consume
    type: 'GET', // The HTTP Method, can be GET POST PUT DELETE etc
    
    dataType: 'json',
  
    success: function(response) { 
       console.log(response);
	   var quote = response.quote;
	   console.log(quote);
 },
    error: function(err) {
		alert(err); 
		},
    beforeSend: function(xhr) {
    xhr.setRequestHeader("X-Mashape-Authorization", "OgGngs3hmYmshg4xvdL6gmKT1Nmkp1nenSpjsn71sOkr8N4qSZ"); // Enter here your Mashape key
    }
});

})

And I get this:

<img src="//cdck-file-uploads-global.s3.dualstack.us-west-2.amazonaws.com/freecodecamp/original/3X/1/f/1f0f69a0a992c0f3a2661fc72201d79189e3d8f8.png" width="690" height="154">
1 Like

Now that Iā€™ve gotten the API to work and can grab the data, I think the rest will be fairly smooth. Got the button working.

var theQuote = '';
var theAuthor = '';
function getQuote(){
$.ajax({
    url: 'https://andruxnet-random-famous-quotes.p.mashape.com/', // The URL to the API. You can get this in the API page of the API you intend to consume
    type: 'GET', // The HTTP Method, can be GET POST PUT DELETE etc
    data: {}, // Additional parameters here
    dataType: 'json',
  
    success: function(data) { 
      
    theQuote = data.quote;
      theAuthor = data.author;
      $('#quote-text').text(theQuote);
      $('#quote-author').text(theAuthor);
    },
    error: function(err) { alert(err); },
    beforeSend: function(xhr) {
    xhr.setRequestHeader("X-Mashape-Authorization", "OgGngs3hmYmshg4xvdL6gmKT1Nmkp1nenSpjsn71sOkr8N4qSZ"); // Enter here your Mashape key
    }
});
}
 $(document).ready(function() {
   getQuote();

    $("#getQuote").on("click", getQuote);

  });

Technically the exercise is done! (but it looks like crap and I want to do more, including the twitter functionality)

I appreciate everyone talking me through this and being patient with my frustrated tirade. Once I took a breath and started playing around it got better.

1 Like

itā€™s ok, I think that from now on youā€™ll be fine because next time youā€™re stuck youā€™ll already know whatā€™s supposed to happen - experimentā€¦

Congrats. Hope you feel just as good as I do, when I manage to fix something that I couldnā€™t get to work for xxx hours :smiley:

Not really in this situation. Thereā€™s still so much I donā€™t know. I can handle APIs from Mashape.com now but I still donā€™t feel like I have a real grasp on APIs in general. Iā€™l feel good when I can create an API from scratch and call from it. Using something like http://docs.mashape.com/javascript#jscript and then just playing around until it works isnā€™t satisfying or why I started FreeCodeCamp. I could already do that. Iā€™ve never had a problem Googling until I find a block of code that does what I want.

Itā€™s like picking a lock by raking the pins versus pushing each one individually. The former requires zero skill and minimum know-how while the latter requires a light touch and an intimate understanding of locks and lock-picking techniques.

Iā€™ll feel good when I can build and pull from an API without the help of Google. Right now I just feel like a hack (aka script kiddy).

Itā€™s a step in the right direction. A baby step, but progress is progress.

You know more than you think you do. jQueryā€™s $.ajax function applies a fair amount of unicorn magic to get things working without much input from you, including parsing the response body as JSON, which may have made this particular problem seem less familiar to you.

I had created a pen a while ago to give students a hands-on, real world challenge in creating an AJAX request. Youā€™ve already gotten past the biggest hurdle there is with this subject (I think, anyways), but if you want to give it a try, please let me know if it helps or how it could be improved.

3 Likes

I appreciate it and will definitely give it a look, although the biggest hurdle for me is making the CSS look halfway decent. I much prefer having a graphic designer create an image and hand it to me. >.<

This is the uninspired, unimaginative dung-heap I have so far. http://codepen.io/FMartin/full/OWJRvP/

There isnā€™t much room for revolution in the world of random quote machines. It works well and you learned a thing. What could be better?

At the very least Iā€™m going to add Twitter integration, which will be more API practice, and Iā€™m thinking about animating the quote text so it types out - Iā€™m sure thereā€™s jquery for that.

1 Like

Iā€™m back trying the Random Quote machine and Iā€™m happy to report that FletcherMartinā€™s code worked when I tried it however, none of my attempts with various quote apis have and I just cannot figure out why! Would someone mind taking a look at my code?

in datatype: 'json' , json must be between quotation marks

1 Like

Thank you so much! I knew it was something stupid. Hopefully now it will be smooth sailing :wink:

I just had to look in the console, it said ā€œjson is not definedā€ :slight_smile: Good luck !

Haha, okay itā€™s not working afterall and I have no idea what it means but at least one less problem to work with!

But if I can get a hint, because I have a feeling you know the answer, itā€™s not a CORS problem, right?