DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

YUI: Parse Json Data From A Async Request

08.14.2008
| 9961 views |
  • submit to reddit
        
var initializeHistoryLog = function(){
	var historyLog = YAHOO.util.Dom.get('historyLog');

	// Define the callbacks for the asyncRequest
	var callbacks = {
		success : function (o) {
			// Process the JSON data returned from the server
			var values = [];
			try {
				values = YAHOO.lang.JSON.parse(o.responseText).ResultSet.Result;
			}
			catch (e) {
				return;
			}

			var el = document.createElement("temp");
			for (var i = 0, len = values.length; i < len; ++i) {
				var v = values[i];
				YAHOO.widget.DataTable.formatDate(el, null, null, YAHOO.util.DataSource.parseDate(v.Date))
				historyLog.innerHTML += '<div class="entry"><div class="date">' + el.innerHTML + ' ' + v.Time + '</div><div class="text"><div class="action">' + v.Action + '</div><div class="changes">' + v.Changes.join('<br>') + '</div></div></div><hr>';
			}
		},

		failure : function (o) {
			if (!YAHOO.util.Connect.isCallInProgress(o)) {
			}
		},

		timeout : 3000
	}

	// Make the call to the server for JSON data
	YAHOO.util.Connect.asyncRequest('GET',"json_GetHistoryLog?openagent&unid=" + unid, callbacks);
}