<< Zune HD | Home | Java web application hosting, why it is so hard? >>

Some thoughts about Ajax

iframe and httpxml

I use to use successfully Ajax iframe based calls, which are quite browser independent and robust. However I met very interesting problem. In most cases my calls returned some HTML content. Sometimes the content was JSON and worked fine as well. Doing some other research I met a problem. JSON returned calls stopped working. JSON structures were messed up and can’t be parsed back. The problem appeared that iframe returned content is retrieving using body.innerHTML, which doesn’t content originally returned data, it got some HTML parsing and normalization touch. I tried to manipulate with content type of response, however it didn’t make things better. So I was trapped and have to switch to use httpxml component based calls. Here I met another problem, if I need to submit form data, I have to do the work exact as browser does for accurate collecting data from all form fields. I was excited to find a ready for use solution. Alas, some crappy code only. So I spend an hour for writing my own implementation which I gladly post here for free. I hate see tons of javascript low quality code posters asking for donation. People, learn to write quality code and you won’t be needed to ask for donation since you can get a decent well paid job after.

function toPostStr(form) {	
	var res = "";
	for ( var i=0; i= 0)
						value = el.options[el.selectedIndex].value;
			} else if (el.type == 'select-multiple') {
				if (el.disabled == false)
					for ( var o in el.options)
						if (o.disabled == false && o.selected) {
							res += el.name + '=' + encodeURIComponent(o.value)
									+ '&';
						}
			}
			if (value) {
				res += el.name + '=' + encodeURIComponent(value) + '&';
			}
		}
	}
	return res;
}




Add a comment Send a TrackBack