postal-codes/script.js

$(function()
{
	$('#form').submit(doSubmit);
	$('#output').hide();
});


function doSubmit(event)
{
	var form = $(this);

	$.ajax({
		url: 'http://fraktguide.bring.no/fraktguide/api/postalCode.json',
		dataType: 'jsonp',
		data: form.serializeObject(),
		success: onReturn,
	});
	return false;
}

function onReturn(data)
{
	$('#output')
		.empty()
		.hide()
		.append(data.result)
		.slideDown();
}


/**
 * Serializes a form into an object.
 */
$.fn.serializeObject = function()
{
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name]) {
			if (!o[this.name].push) {
				o[this.name] = [o[this.name]];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
};