Posted By

StrawMan on 09/04/12


Tagged

javascript json


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

spee66


JSONObj


 / Published in: jQuery
 

URL: http://www.mobiledesign.com

Json data handling object. Provides two static methods for handling json data retrieval in conjunction with the jQuery ajax and json parser.

  1. ///////////////////////////
  2. //JSONObj.js
  3. //JSON data handler.
  4. ///////////////////////////
  5. //JSON data handling object.
  6. JSONObj = {
  7. getdataurl: function(url, type, data) {
  8. var jqxhr = null;
  9. var type_string = (!type) ? 'GET' : type;
  10. var dat = (data === null) ? null : data;
  11. //AJAX defaults.
  12. $.ajaxSetup({
  13. url: url,
  14. global: true,
  15. type: type_string,
  16. data: dat,
  17. dataType: 'json',
  18. cache: false
  19. });
  20. //Return data.
  21. jqxhr = $.ajax();
  22. return jqxhr;
  23. },
  24. getdatavar: function(variable) {
  25. //Parse JSON data from variable.
  26. var json = jQuery.parseJSON(variable);
  27. //Return data.
  28. return json;
  29. }
  30. };
  31. ///////////////////////////
  32.  
  33. ///////////////////////////
  34. //Example:
  35. ///////////////////////////
  36. var jsondata = new JSONObj.getdataurl('http://your/data/url');
  37.  
  38. $.when(jsondata).then(function(json){
  39. doSomethingWithIt(json);
  40. })
  41. .fail(function(){
  42. console.log('JSON Data not received, halt...');
  43. });
  44. ///////////////////////////

Report this snippet  

You need to login to post a comment.