AJAX Library (ajaxlib.js)

These are functions commonly used in AJAX. createXMLHttp() returns an XMLHttpRequest object to use in AJAX communications (compatible with IE5.5). buildPostBody() returns a body built from a form object to use in posting using the AJAX XMLHttpRequest object. buildFormData() is used for posting "multipart/form-data." See ASP.NET MVC AJAX File Upload for an example using buildFormData().

//ajaxlib.js
function createXMLHttp(){
	if(typeof XMLHttpRequest != "undefined"){
		return new XMLHttpRequest();
	} else if(window.ActiveXObject){
		var aVersions = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
		for(var i = 0; i < aVersions.length; i++){
			try{
				var oXmlHttp = new ActiveXObject(aVersions[i]);
				return oXmlHttp;
			}catch(oError){
				//void
			}
		}
	}
	throw new Error("XMLHttp object could not be created.");
}
function buildPostBody(form_object) {
	var params = new Array();
	var len = form_object.elements.length;
	for (var i = 0; i < len; i++) {
		var field = form_object.elements[i];
		if (field.name != null && field.name.length > 0) {
			switch (field.type) {
				case "select-one":
				case "select-multiple":
					if (field.name.length) {
						var j, optLen;
						for (j = 0, optLen = field.options.length; j < optLen; j++) {
							option = field.options[j];
							if (option.selected) {
								var optValue = "";
								if (option.hasAttribute) {
									optValue = (option.hasAttribute("value") ? option.value : option.text);
								} else {
									optValue = (option.attributes["value"].specified ? option.value : option.text);
								}
								params.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(optValue));
							}
						}
					}
					break;
				case "submit":
				case "reset":
				case "button":
				case undefined:
				case "file":
					break;
				case "checkbox":
				case "radio":
					if (!field.checked) {
						break;
					}
				default:
					params.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(field.value));
			}
		}
	}
	return params.join("&");
}
function buildFormData(form_object) {
	var fd = new FormData();
	var len = form_object.elements.length;
	for (var i = 0; i < len; i++) {
		var field = form_object.elements[i];
		if (field.name != null && field.name.length > 0) {
			switch (field.type) {
				case "select-one":
				case "select-multiple":
					if (field.name.length) {
						var j, optLen;
						for (j = 0, optLen = field.options.length; j < optLen; j++) {
							option = field.options[j];
							if (option.selected) {
								var optValue = "";
								if (option.hasAttribute) {
									optValue = (option.hasAttribute("value") ? option.value : option.text);
								} else {
									optValue = (option.attributes["value"].specified ? option.value : option.text);
								}
								fd.append(field.name, optValue);
							}
						}
					}
					break;
				case "submit":
				case "reset":
				case "button":
				case undefined:
					break;
				case "file":
					for (var j = 0; j < field.files.length; j++) {
						fd.append(field.name, field.files[j], field.files[j].name)
					}
					break;
				case "checkbox":
				case "radio":
					if (!field.checked) {
						break;
					}
				default:
					fd.append(field.name, field.value)
			}
		}
	}
	return fd;
}