Calling non-origin server

Giganews Newsgroups
Subject: Calling non-origin server
Posted by:  Brian Adkins (lojicdotc…@gmail.com)
Date: Fri, 02 Nov 2007

I need to have a script communicate with a server other than the one
it was loaded from. I've come up with the function below (slightly
modified for posting), but I thought I'd ask for constructive
criticism.

function transmitToServer(method, parameters) {
  var s = document.createElement('script');
  s.type = 'text/javascript';
  s.charset = 'utf-8';
  s.src = getServerURL() + method;
  var sep = '?';
  parameters.nocache = (new Date()).getTime();
  for (var prop in parameters) {
    if (parameters[prop] != null) {
      s.src += sep + prop + '=' + escape(parameters[prop]);
      if (sep === '?') { sep = '&'; }
    }
  }
  document.body.appendChild(s);
}

An example invocation is:

transmitToServer('my_action', { name:  'Brian', phone:
'800-555-1212' });

Brian Adkins

Replies