Re: Controlling Javascript from server side



Bart Van der Donck wrote:
Ajax is very suitable for this kind of interaction.

A Python programme 'file.py':

#!/usr/bin/env python
print "Content-type: text/html\n\n"

The "Content-Type" header value of a HTTP message should always contain a
declaration of the encoding of the message body ("charset=...").

import random
print random.randrange(50)

(for the sake of simplicity, this just reurns a random number 0-50)

A javascript programme:

It is merely ECMAScript-3-conforming code, or such a script or program.
It may seem like nitpicking, but it is very important to understand that
whether it is JavaScript or not is defined by the execution environment,
that is the ECMAScript *implementation*, the script engine that is used
to execute the code. For example, it would be Netscape JavaScript in NN2+
and Gecko-based UAs, Microsoft JScript in MSHTML (IE-based), Opera
ECMAScript in Opera, KJS ECMAScript in Konqueror, and JavaScriptCore
JavaScript in Safari. (Hence my use of the terms "J(ava)Script/ECMAScript"
or "JS/ES".)

See also http://PointedEars.de/scripts/es-matrix for differences between
some ECMAScript implementations. (I will add more implementations there;
your input, as always, is welcome).


var _global = this;

function ajax(url, vars, callbackFunction) {
if (window.XMLHttpRequest) {

Wrong feature test. Should be:

if (/\b(function|object)\b/i.test(typeof XMLHttpRequest)
&& _global.XMLHttpRequest)

var request = new XMLHttpRequest();

One wants to attempt exception handling here in the case this fails anyway.

}
else {
var request = new ActiveXObject('MSXML2.XMLHTTP.3.0');

Contrary to what the MSDN Library says, it is not necessary here to force a
specific (the newest) version of MSXML or IXMLHTTPRequest; all used
properties are available ever since. The required exception handling should
be added in the case this fails.

}
request.open('GET', url, true);

AFAIK the third argument is unnecessary here, although may be used to
indicate the default value (`true').

http://msdn2.microsoft.com/en-us/library/ms757849.aspx
http://xulplanet.com/references/objref/XMLHttpRequest.html#method_open

request.setRequestHeader('Content-Type', '');

That would make the HTTP request invalid, as the header name "Content-Type"
is inappropriate at least for a *GET* request. (HTTP/1.x does not define
such a request header for any request type, only a response header.[1]
[CMIIW] However, to submit different types of information on POST requests,
HTML 4.01 specified implicitly that the Content-Type header may be used also
for request messages. The default used for submitting forms with POST is
specified as "application/x-www-form-urlencoded" which usually suffices.[2]
It is not used by the interface for GET; those requests have no Content-Type
header by default.)

And, more important, the header value must not be empty. The interface does
not specify a default value to be used when the second parameter is the
empty string.

That method call in general will also throw an exception with some builds,
IIRC of Opera.

[1] http://www.rfc-editor.org/rfc/rfc1945.txt
http://www.rfc-editor.org/rfc/rfc2616.txt
[2] http://www.w3.org/TR/html401/interact/forms.html#form-content-type

request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {

Other final response status codes than `200' are possible for a successful
request, see HTTP/1.x and the interface specification.

if (request.responseText) {

That test would only handle the case (and improve efficiency a bit with the
code below) if the response body would be empty. It can be safely omitted.

document.getElementById('myField').value =

Missing feature test, therefore error-prone referencing.

(request.responseText).replace(/(\r\n|\r|\n|\s)/g, '');

There is no need for parantheses around the object reference, and the
Regular Expression can be written more efficient (matching the same characters):

... = request.responseText.replace(/\s+/g, '');

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegExp#Special_characters_in_regular_expressions

}
}
}
request.send(vars);

This assumes a POST request where the argument of IXMLHTTPRequest::send()
specifies the message body. However, that is not the case here (it's GET),
so the value should be `undefined' (i.e. not passed at all), or `null' to be
compatible to the Gecko DOM. That decision should not be left to the caller
of ajax().

// perform new call every 2 seconds (2000 milliseconds)
setTimeout("ajax('file.py?' + Math.random(), '', '')", 2000);
}
document.write('Status value: <input type="text" id="myField">');

If a `form' element would be written here that contained the `input'
element, the feature test for D::gEBI() above would be unnecessary.

ajax('file.py?' + Math.random(), '', '');

Given the nature of random numbers in general, and especially
machine-generated random numbers, this will not avoid a cached
resource to be retrieved, which was probably the intention.
The following would:

ajax('file.py?' + (new Date()).getTime(), '', '');


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
.



Relevant Pages

  • Re: urllib2 content-type headers
    ... This API requires an HTTP header to be set as follows: ... you cannot rely on Content-Type being correct ... Try sending the request to a server you control so you can see what it is actually receiving. ...
    (comp.lang.python)
  • [REVS] Forging HTTP Request Headers with Flash ActionScript
    ... Forging HTTP Request Headers with Flash ActionScript ... A similar syntax will send POST request (with the same header, ...
    (Securiteam)
  • Write-up by Amit Klein: "Forging HTTP request headers with Flash"
    ... Forging HTTP request headers with Flash ... A similar syntax will send POST request (with the same header, ...
    (Bugtraq)
  • Re: if-modified-since question (protocol problem?)
    ... request, but in response you are interested only if your request ... To base the decision on whether there is something 'new to show' would require knowledge about something 'old' - a cached resource. ... No Last-Modified header is sent in the response, ...
    (comp.infosystems.www.authoring.html)
  • Re: Controlling Javascript from server side
    ... declaration of the encoding of the message body. ... the expected encoding is always ISO/IEC ... 'true' means that the request must be handled asynchronously. ... (HTTP/1.x does not define such a request header for any request ...
    (comp.lang.javascript)

Quantcast