.getJSON() error when crlf doesn't match os's liking
- function getRandomQuote() {
- var quotes = [], randomIndex, quote;
- $.getJSON('/quotes.json', function(data) { //http://api.jquery.com/jQuery.getJSON/
- $.each(data, function(quote, cite) {
- if ('' != quote && '' != cite) {
- quotes.push(quote + '|||' + cite);
- }
- });
- })
- .done(function() { console.log( "JSON success" ); })
- .fail(function() { console.log( "JSON error" ); })
- .always(function() { console.log( "JSON complete" ); });
- //pick a random quote from the total number of quotes
- randomIndex = parseInt(Math.random() * quotes.length);
- //separate out quote and cite from array element
- quote = quotes[randomIndex].split('|||'); //index 0 is "quote", index 1 is "cite"
- document.getElementById('ip').innerHTML = quote[0];//"quote":
- document.getElementById('icite').innerHTML = quote[1];//"cite":
- return quote;
- }
- getRandomQuote();
this bug needs to be fixed so it works regardless of whether the file has lf or crlf for EOL or even a mixture. it's doable. I am using ff, but I could try testing with other browsers if need be.
also, I always get an error with this code, even with success, because randomIndex is always 0 and quotes.length is always 0. I have no idea what's going on behind the scenes now that I am getting
JSON success
JSON complete
I just know that for some reason, .each is not working.