{"id":764,"date":"2010-02-22T17:29:12","date_gmt":"2010-02-22T09:29:12","guid":{"rendered":"http:\/\/blog.lolyco.com\/sean\/?p=764"},"modified":"2010-02-22T17:29:12","modified_gmt":"2010-02-22T09:29:12","slug":"ajax-enabled-exchange-rates","status":"publish","type":"post","link":"https:\/\/blog.lolyco.com\/sean\/2010\/02\/22\/ajax-enabled-exchange-rates\/","title":{"rendered":"AJAX-enabled exchange rates"},"content":{"rendered":"<p>I finally got round to adding client-side scripting to the <a href=\"http:\/\/localhost:22791\/currency-conversion.html\">Currency Conversion page at spider.my<\/a>! <a href=\"http:\/\/en.wikipedia.org\/wiki\/AJAX\">AJAX<\/a> is &#8216;Asynchronous Java And XML&#8217;. Basically, it allows you to embed some functionality in your web page so that your page can communicate with your server after it has been rendered in your visitor&#8217;s we browser, but without reloading the entire page.<\/p>\n<p>I want to use AJAX for some other projects, so I did <a href=\"http:\/\/www.w3schools.com\/jsref\/default.asp\">some research<\/a> this morning and used the Currency Conversion page to test it out on. I previously wrote a plain text exchange rate query facility at spider.my which returns only the rate and nothing else. It&#8217;s not entirely straightforward to retrieve plain text data using javascript, as far as I can tell. The <a href=\"http:\/\/www.w3schools.com\/XML\/xml_http.asp\">XMLHttpRequest<\/a> object that javascript uses to make web requests will only work if it receives a response with a valid XML type, so my text\/plain initial effort just isn&#8217;t good enough!<\/p>\n<div id=\"attachment_765\" style=\"width: 410px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/blog.lolyco.com\/sean\/wp-content\/uploads\/2010\/02\/spider.my_.ccxml_.jpeg\"><img decoding=\"async\" aria-describedby=\"caption-attachment-765\" class=\"size-full wp-image-765 \" title=\"A little bit of AJAX on my Currency Conversion page!\" src=\"http:\/\/blog.lolyco.com\/sean\/wp-content\/uploads\/2010\/02\/spider.my_.ccxml_.jpeg\" alt=\"A little bit of AJAX on my Currency Conversion page!\" width=\"400\" \/><\/a><p id=\"caption-attachment-765\" class=\"wp-caption-text\">A little bit of AJAX on my Currency Conversion page!<\/p><\/div>\n<p>If you visit spider.my&#8217;s currency conversion page now, you&#8217;ll see that the current exchange rate for the two selected currencies is displayed next to the &#8216;Convert&#8217; button (see image above). Using &#8216;onchange&#8217; events in the select elements, the page now invokes a javascript function that requests the latest exchange rate from spider.my and updates the displayed rate. That&#8217;s a very simple example, but similar code could easily give you an exchange rate widget on your own web pages.<\/p>\n<p>What happens when the selected currencies are changed is that the javascript in the page sends a request to spider.my for the appropriate exchange rate. For example, <a href=\"http:\/\/localhost:22791\/exchange-rate\/GBP-USD.xml\">http:\/\/localhost:22791\/exchange-rate\/GBP-USD.xml<\/a> is the URL requested for the UK Sterling \/ US Dollar rate. It&#8217;s a very simple response &#8211; here it is:<\/p>\n<p><code>&lt;exchangerate rate=\"1.5392\" source=\"http:\/\/www.ecb.int\/stats\/eurofxref\/eurofxref.zip (Date=19 February 2010)\"&gt;<\/code><\/p>\n<p>All that the javascript has to do is to extract the rate part of the XML response and update the div that holds the visible exchange rate:<\/p>\n<p><code>var rate = xmlResponse.getElementsByTagName(<br \/>\n\"exchangerate\")[0].getAttribute(\"rate\");<br \/>\ndivRate.innerHTML = '(@'+rate+')';<\/code><\/p>\n<p>Easy huh?<\/p>\n<p>I&#8217;ve got a better example coming up in the next day or two &#8211; stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I finally got round to adding client-side scripting to the Currency Conversion page at spider.my! AJAX is &#8216;Asynchronous Java And XML&#8217;. Basically, it allows you to embed some functionality in your web page so that your page can communicate with your server after it has been rendered in your visitor&#8217;s we browser, but without reloading [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,60,3,35],"tags":[21,41,34,18,105,57],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-fixed","category-money","category-software","category-spidermy","tag-e-commerce","tag-http","tag-network","tag-server","tag-software","tag-web"],"_links":{"self":[{"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/posts\/764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/comments?post=764"}],"version-history":[{"count":6,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":771,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/posts\/764\/revisions\/771"}],"wp:attachment":[{"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lolyco.com\/sean\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}