Help with overcoming cross site AJAX request problem
Posted: Sun Jan 17, 2010 9:04 pm
Hi all,
Please don't flame me for being a newbie in open source. I have used Microsoft programming languages, Cobol, Fortran, dBase and now am stuck with a LAMP problem needing some javascript and php help.
I know I must spend the time studying, but I have a serious time constraint on this task. I have been developing applications for 20 years and understand the necessary learning process.
But I am stuck on what at first seemed to be an easy thing to do.
I am trying to use the site govtrack.us to get XML returned from a form my users submit. If you put this URL...
"http://www.govtrack.us/perl/district-lo ... code=06424"
in your browser you get returned some nice and useful XML.
<congressional-district>
<session>110</session>
<state>CT</state>
<district>2</district>
<member type="senator" id="300034"/>
<member type="senator" id="300067"/>
<member type="representative" id="412193"/>
</congressional-district>
I am trying to obtain this XML using an AJAX request with JavaScript as shown below.
However, I found out that security will not allow this code to run across domains.
At the line:
xhttp.open("GET",url,true);
I am getting "access denied".
It was mentioned that I could instead place the AJAX call to a PHP script on my server that then "somehow" pulls that same XML info from the remote site and presents it to the calling JavaScript. I tried using simply...
<?php
/* Redirect browser */
header("http://www.govtrack.us/perl/district-lo ... code=06424");
?>
In a file called gtk_get.php and calling that as the url in the line:
xhttp.open("GET",url,true);
But that seems not to work either.
//===========================
function getDistByZip_sav(zip5, zip4)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else // Internet Explorer 5/6
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//-- Set up the URL text
url="http://www.govtrack.us/perl/district-lo ... code=06424";
xhttp.onreadystatechange=handlerZip;
xhttp.open("GET",url,true);
xhttp.send(null);
}
//=============================
Can anyone please give me a clue as to how I can "call" that url ("http://www.govtrack.us/perl/district-lo ... code=06424") from the server in PHP code and then pass it back to the JavaScript AJAX request so I can parse it and use it to construct some dynamic HTML in the page making the request?
Thanks for any help.
Please don't flame me for being a newbie in open source. I have used Microsoft programming languages, Cobol, Fortran, dBase and now am stuck with a LAMP problem needing some javascript and php help.
I know I must spend the time studying, but I have a serious time constraint on this task. I have been developing applications for 20 years and understand the necessary learning process.
But I am stuck on what at first seemed to be an easy thing to do.
I am trying to use the site govtrack.us to get XML returned from a form my users submit. If you put this URL...
"http://www.govtrack.us/perl/district-lo ... code=06424"
in your browser you get returned some nice and useful XML.
<congressional-district>
<session>110</session>
<state>CT</state>
<district>2</district>
<member type="senator" id="300034"/>
<member type="senator" id="300067"/>
<member type="representative" id="412193"/>
</congressional-district>
I am trying to obtain this XML using an AJAX request with JavaScript as shown below.
However, I found out that security will not allow this code to run across domains.
At the line:
xhttp.open("GET",url,true);
I am getting "access denied".
It was mentioned that I could instead place the AJAX call to a PHP script on my server that then "somehow" pulls that same XML info from the remote site and presents it to the calling JavaScript. I tried using simply...
<?php
/* Redirect browser */
header("http://www.govtrack.us/perl/district-lo ... code=06424");
?>
In a file called gtk_get.php and calling that as the url in the line:
xhttp.open("GET",url,true);
But that seems not to work either.
//===========================
function getDistByZip_sav(zip5, zip4)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else // Internet Explorer 5/6
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//-- Set up the URL text
url="http://www.govtrack.us/perl/district-lo ... code=06424";
xhttp.onreadystatechange=handlerZip;
xhttp.open("GET",url,true);
xhttp.send(null);
}
//=============================
Can anyone please give me a clue as to how I can "call" that url ("http://www.govtrack.us/perl/district-lo ... code=06424") from the server in PHP code and then pass it back to the JavaScript AJAX request so I can parse it and use it to construct some dynamic HTML in the page making the request?
Thanks for any help.