site logo

How to read data from server to server by help of AJAX

On example of JavaScript calculator, which keeps up-to-date currency rate, I'm going to demonstrate how to read and use data located on remote web server. Mini-application uses Ajax object on server for reading actual currency rate from server situated in another domain and appends list of coefficients for calculator. Updated calculator is sended to the browser then.

Ajax in browser is limited to communicate with servers which are in the same domain from which current page in browser originates. Whenever you want to read data from server, which is in another domain, object Ajax must run on server, where this limitation is not applied. Application which is running on intranet server behind the firewall needs also permission on firewall for communication with given remote server.

Czech Crown (CZK) is presented because it is local currency in my country. Currency rate originates from Czech National Bank and is official rate for next day (days in case of holidays). Rates from other resources (Google) may be slightly different. It is not floating rate from Stock Exchange which changes every minute.

By clik on table row select conversion type, then drag pointer on pseudo-logarithmic scale to desired value (in left form field). Right field displays matching converted value. It is possible to enter digital value directly to the input fields (left, or right). Then click on mark "equal" to calculate converted value.

selection source unit target unit rate updated
 ➨ ➨ ➨ 
EUR CZK 24,105 26.04.2011
 ➨ ➨ ➨ 
USD CZK 16,491 26.04.2011
 ➨ ➨ ➨ 
inch cm 2.54  
 ➨ ➨ ➨ 
HP kW 1.34102  
source unit = target unit

Object XMLHTTPRequest on server can read any file from remote web server, but is designed for reading .xml, or .txt files. Real sense of reading .html file is very limited. Automatic analysis of html code is difficult, just small change in source code breaks up analyse and data are invalid, or none at all.

Practical utilization of XMLHTTPRequest object on server is for reading web data, which changes in time. For example currency rate, weather forecast, or production data on intranet. It is also possible to use that object for test whether web server is currently running (ping is not always sufficient).

Web server of Czech National Bank publishes daily rate of exchange, formated in standard .html code for display in browser and the same data in .txt file, formated as ASCII table. That .txt file is right for reading by XMLHttpRequest object. ASCII table contains 35 different types of rate. Each row contains data for country, currency, quantity, code and rate. Single items are separated by character "pipe".

In code example is part of server code of just displayed page, written in VBScript for classic.asp which runs in MS IIS server only. That function is not portable to other server type. ServerXMLHTTP object is other than the object used in browser for Ajax, but provides the same service.

Function reads remote .txt file of exchange rate. Code of synchronous request which is used here is plain, simpler than asynchronous code for Ajax request in browser. Synchronous request causes suspension of program until data transmission is finished. In case of browser that would result in browser stops react. That fact does not matter on server, only page responses somewhat for a longer time. Besides, there is no other possibility on server. In case of synchronous request, abbreviation AJAX is not correct, accurate could be SJAX: Synchronous JavaScript and XML.


FUNCTION updateRate()
  ' ===== updating currency rate =====
  Dim HTTP, Addr

  ' ----- get txt file from remote server by help of AJAX -----
  Addr = "http://www.cnb.cz/cs/.../denni_kurz.txt"
  Set HTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
  HTTP.Open "GET", Addr, false, "", ""
  HTTP.setRequestHeader "Content-Type", "text/plain"
  HTTP.Send()
  If (HTTP.status = 200) Then
     ' ----- data processing, update coefficients -----
     ' Lines = Split(HTTP.responseText, vbLf)
     ' ...
  End If
  Set HTTP=Nothing
END FUNCTION

Used variables are declared at first. Addr contains URL address of page on remote server, variable HTTP is for object XMLHTTP. This object is opened with values: method of request, URL address, method of processing (here false means synchronous processing). More parameters are used only in case of login name and password demand. Next, response header is set up. Here simple text file is used, therefore text/plain is set up, and finally request is sent. At this point program stops its execution until response is delivered. As soon as program restarts we have to test if required file was really delivered. It is ensured by object status HTTP.status = 200 and data processing is done only in case error free transmission. If any problem occures, default data are used.

Analysis and data processing depends on file structure, therefore is not in listing. In general, received data are separated by function split to single items and according to currency code desired rates are inserted as new coefficients into JavaScript array which is sent to browser. Effective update is identified by the date in table. When update is not successful, default (old) values are used.

Using scale and slider for setting values was described in article forms, date and time, in case of need, check source code of this page please.

updated 10.02.2008