Created
March 5, 2014 03:44
-
-
Save kylerush/9360760 to your computer and use it in GitHub Desktop.
Code example for a blog post "Tips for consuming JSON APIs in JavaScript."
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery.get('/account/info', function(data){ | |
| jQuery('#user-name').text(data.userName); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery.get('/account/info').always(function(data, textStatus, jqXHR){ | |
| if(jqXHR.status === 200){ | |
| try{ | |
| if(typeof data.userName === 'string'){ | |
| jQuery('#user-name').text(data.userName); | |
| } else { | |
| //send data to google analytics | |
| _gaq.push(['_trackEvent', 'api error', 'api_name', 'typeof data.userName: ' + typeof(data.user.name)]); | |
| } | |
| } catch(error){ | |
| //handle the error, update the DOM to inform user the api is down | |
| //send data to google analytics | |
| _gaq.push(['_trackEvent', 'api error', 'api_name', 'response contains invalid JSON')]); | |
| } | |
| } else { | |
| //send data to google analytics | |
| _gaq.push(['_trackEvent', 'api error', 'api_name', 'status code: ' + jqXHR.status]); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery.get('/account/info').always(function(data, textStatus, jqXHR){ | |
| if(jqXHR.status === 200){ | |
| jQuery('#user-name').text(data.userName); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery.get('/account/info').always(function(data, textStatus, jqXHR){ | |
| if(jqXHR.status === 200){ | |
| try{ | |
| jQuery('#user-name').text(data.userName); | |
| } catch(error){ | |
| //handle the error, update the DOM to inform user the api is down | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery.get('/account/info').always(function(data, textStatus, jqXHR){ | |
| if(jqXHR.status === 200){ | |
| try{ | |
| if(typeof data.userName === 'string'){ | |
| jQuery('#user-name').text(data.userName); | |
| } | |
| } catch(error){ | |
| //handle the error, update the DOM to inform user the api is down | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment