[Javascript] ajax- demo

Matt Murphy matt.murphy at thermofisher.com
Tue Mar 13 14:28:48 CDT 2007


I think you can make this work *IF* the webservers are both within the
same domain. 

You'd have a page on serverone.mydomain.com that has the javascript call
in it:

Onclick="ajaxme('http://servertwo.mydomain.com/script.php', data)";

(data would be something like
document.getElementById('domelement').value)

In your html somewhere you'd have an ajax function defined like:

<script language="Javascript">

function ajaxme(url,data)
{
        http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new
ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance. Your
browser may be too old for this application.');
            return false;
        }
        http_request.onreadystatechange = hitMe;
        http_request.open('POST', url, true);
        http_request.send(data);
    }



function hitMe()
{
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                eval(http_request.responseText);
            } else {
                alert('ajaxme: There was a problem with the
request.'+http_request.statusText+' - '+http_request.responseText);
            }
        }
    }

</script>

And on your servertwo script, you'd do whatever processing you need to
do with that data, then  return javascript functions that would update
DOM elements in the page on server1. In PHP it would look something
like:

$data = $HTTP_RAW_POST_DATA;

// whatever validation, database, or processing stuff you want

?>
document.getElementById('domelement').innerHTML="<?=$return?>";
<?php

Does this make sense? I *think* the example I've given should fit the
requirement of javascript working within the same domain, but I've never
tried this. 

Matt



More information about the Javascript mailing list