big_red
Dec 14th 2007, 10:52 pm
Hello ...
I'm virtually new to this whole js thing, and I've been wrestling with this problem for a while now. I'm trying to get some data from an external server. I have some code that works perfectly when run locally, but doesn't work when I upload to my server. I understand that may be do to cross-server security issues (although my script just does nothing when it's on my server ... I don't get any errors). I'm completely lost and thought I'd try to post the code here to see if there's another way / more simple way to get done what I'm trying to do.
btw, this is supposed to lookup an address (in utah) from one of the state's servers, and return who the legislators are for that address. To test, you can use an address of 100 S Main St; Salt Lake City, 84101. Oh, and because of the server I'm using, PHP would be a last option ... but if it's the only way, I'll take what I can get :-) Thanks!
...
<HTML>
<HEAD>
<TITLE>Utah State Legislator Lookup</TITLE>
<script language="JavaScript">
<!--
function waitForResponse()
{
if (http_request.readyState == 4)
{
waitCount = 0
if (http_request.status == 200)
{
test = http_request.responseText;
setInst('');
//
if(test.indexOf("<RESULT>")>-1)
{
startPos = test.indexOf("<RESULT><![CDATA[");
endPos = test.indexOf("]]></RESULT>",startPos + 1);
resultData = test.substring(startPos+17, endPos);
txt = document.getElementById("results");
txt.innerHTML = resultData;
}
else
{
// error
return
}
}
}
else // not ready yet
{
return
}
}
function setInst(newText)
{
txt = document.getElementById("instruct")
txt.innerHTML = newText
}
function doSearch()
{
txt = document.getElementById("results");
addressVal = document.form1.address.value
cityVal = document.form1.city.value
zipVal = document.form1.zip.value
if(addressVal=="" && cityVal=="" && zipVal=="")
{
txt.innerHTML = ''
setInst('<FONT face="Arial" size=-1 color=red>Please enter an address and zip code or city.</FONT>')
return;
}
// send request off to server
var url = "http://le.utah.gov/GIS/GISLNK?function=lookup&address=" + addressVal + "&city=" + cityVal + "&zip=" + zipVal
http_request = false;
if (window.XMLHttpRequest)
{
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject)
{
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{}
}
}
if (!http_request)
{
errStr = 'Giving up :( Cannot create an XMLHTTP instance)'
return;
}
http_request.onreadystatechange = waitForResponse;
http_request.open('GET', url, true);
http_request.send(null);
}
//-->
</script>
</HEAD>
<BODY>
<FORM name="form1">
<TABLE width="100%">
<TR>
<TD valign=top>
Address
<BR>
<INPUT type=text name="address" size=25>
<BR>
City
<BR>
<INPUT type=text name="city" size=25>
<BR>
Zip Code
<BR>
<INPUT type=text name="zip" size=5><INPUT type=button name="gobtn" Value="Search" onClick="doSearch()">
</TD>
</TR>
<TR>
<TD valign=top>
<DIV>
<span id="results"></span>
<span id="instruct"></span>
</DIV>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
I'm virtually new to this whole js thing, and I've been wrestling with this problem for a while now. I'm trying to get some data from an external server. I have some code that works perfectly when run locally, but doesn't work when I upload to my server. I understand that may be do to cross-server security issues (although my script just does nothing when it's on my server ... I don't get any errors). I'm completely lost and thought I'd try to post the code here to see if there's another way / more simple way to get done what I'm trying to do.
btw, this is supposed to lookup an address (in utah) from one of the state's servers, and return who the legislators are for that address. To test, you can use an address of 100 S Main St; Salt Lake City, 84101. Oh, and because of the server I'm using, PHP would be a last option ... but if it's the only way, I'll take what I can get :-) Thanks!
...
<HTML>
<HEAD>
<TITLE>Utah State Legislator Lookup</TITLE>
<script language="JavaScript">
<!--
function waitForResponse()
{
if (http_request.readyState == 4)
{
waitCount = 0
if (http_request.status == 200)
{
test = http_request.responseText;
setInst('');
//
if(test.indexOf("<RESULT>")>-1)
{
startPos = test.indexOf("<RESULT><![CDATA[");
endPos = test.indexOf("]]></RESULT>",startPos + 1);
resultData = test.substring(startPos+17, endPos);
txt = document.getElementById("results");
txt.innerHTML = resultData;
}
else
{
// error
return
}
}
}
else // not ready yet
{
return
}
}
function setInst(newText)
{
txt = document.getElementById("instruct")
txt.innerHTML = newText
}
function doSearch()
{
txt = document.getElementById("results");
addressVal = document.form1.address.value
cityVal = document.form1.city.value
zipVal = document.form1.zip.value
if(addressVal=="" && cityVal=="" && zipVal=="")
{
txt.innerHTML = ''
setInst('<FONT face="Arial" size=-1 color=red>Please enter an address and zip code or city.</FONT>')
return;
}
// send request off to server
var url = "http://le.utah.gov/GIS/GISLNK?function=lookup&address=" + addressVal + "&city=" + cityVal + "&zip=" + zipVal
http_request = false;
if (window.XMLHttpRequest)
{
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject)
{
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{}
}
}
if (!http_request)
{
errStr = 'Giving up :( Cannot create an XMLHTTP instance)'
return;
}
http_request.onreadystatechange = waitForResponse;
http_request.open('GET', url, true);
http_request.send(null);
}
//-->
</script>
</HEAD>
<BODY>
<FORM name="form1">
<TABLE width="100%">
<TR>
<TD valign=top>
Address
<BR>
<INPUT type=text name="address" size=25>
<BR>
City
<BR>
<INPUT type=text name="city" size=25>
<BR>
Zip Code
<BR>
<INPUT type=text name="zip" size=5><INPUT type=button name="gobtn" Value="Search" onClick="doSearch()">
</TD>
</TR>
<TR>
<TD valign=top>
<DIV>
<span id="results"></span>
<span id="instruct"></span>
</DIV>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>