I needed to make HTTP posts of some information for an Ajax chat module I'm testing, so I've added an HTTPPost() function to drupal.js.

It submits information like an HTML form would (using x-www-form-urlencoded). I'm not sure if assuming all HTTPosts will be encoded in this way is the best approach. I've never used any other encoding, but that's just me.

Comments

Thox’s picture

StatusFileSize
new1.29 KB

I've improved the coding style for a new patch.

altano’s picture

I'm developing an AJAX chat client for fun and I've come across a lot of issues with XMLHttpRequest.

First off, every access of the object should be in a try catch block, as exceptions can be thrown (especially with the send method) for no decipherable reason. You should handle these errors as well (a failed send will require recreation of the XMLHttpRequest object... for some reason trying to reuse them presents problems). I just have a function that creates the object and call it as needed.

Here is some code for initializing a more cross-browser XMLHttpRequest object which can be thrown into a function:

	var xmlhttp;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
		try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
		xmlhttp = false;
		}
		}
	@end @*/
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
		xmlhttp.overrideMimeType('text/xml');
	}

I honestly don't think AJAX is ready yet (for whatever), but I thought I would try to be somewhat informative.

Thox’s picture

I've got a working Drupal Ajax chat demo (using POST and GET requests) here:
http://brandedthoughts.co.uk/chat

I've not (yet) had any unexpected errors thrown.

altano’s picture

Depending on how often you poll for data I can see it not being an issue.

I had mine going with 50ms delays and left it overnight before I saw the errors start to pop up. Call me crazy =\

Thox’s picture

StatusFileSize
new1.31 KB

My latest ajax-powered spellcheck module requires HTTPPost too. I'm keen to see this function in core, as opposed to putting it in the module itself.

I've updated it to allow sending of raw text to the recieving page, where before it always encoded the text/object as form encoding.

Thox’s picture

StatusFileSize
new2.72 KB

New patch, improved coding style of the whole drupal.js file.

dries’s picture

Status: Needs review » Fixed

Committed to HEAD.

Anonymous’s picture

Anonymous’s picture

osherl’s picture

Anonymous’s picture

Anonymous’s picture

Anonymous’s picture

Status: Fixed » Closed (fixed)