Help with overcoming cross site AJAX request problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
jkotuby
Forum Newbie
Posts: 7
Joined: Sun Jan 17, 2010 8:37 pm

Help with overcoming cross site AJAX request problem

Post by jkotuby »

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.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Help with overcoming cross site AJAX request problem

Post by kaszu »

This should be done not with header("Location: "), but with curl or file_get_contents, see what google says: http://www.google.com/search?source=ig& ... ax%20proxy
jkotuby
Forum Newbie
Posts: 7
Joined: Sun Jan 17, 2010 8:37 pm

Re: Help with overcoming cross site AJAX request problem

Post by jkotuby »

Thanks kaszu,

Sometimes finding the right answer requires knowing the right question to ask. The keyword search "php ajax proxy" produced the goldmine of information I was seeking.

Thanks for your help and have a great day!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Help with overcoming cross site AJAX request problem

Post by Eran »

An alternative solution is to use JSONP which allows for cross-domain inline retrieval. I suggest you read about that too
jkotuby
Forum Newbie
Posts: 7
Joined: Sun Jan 17, 2010 8:37 pm

Re: Help with overcoming cross site AJAX request problem

Post by jkotuby »

@pytrin
Thanks for the lead to JSONP. From what I have read so far that technique requires that the remote server has set up the facility for returning JSON formatted data placed in a javascript wrapper. I may be wrong, but that is my understanding so far.

The site I am tapping into has API's set up for searches using ZIP, ZIP+4, State, Street Address and Latitiude/Longitude (to be used in conjunction with Google Maps --sweet!)

However the API's return raw XML.
The webmaster does expose the folder hierarchy of availalbe XML data files, which he encourages other webmasters to tap into for theit own sites. As my most recent experience has been with ASP.NET web applications, using Javascript and even more so PHP on the backend is very foreign to me (and frustrating), but I am beginning to pick up momentum.

I have been studying JS and XML and PHP on w3schools.com tutorials. I must learn what amounts to a few college level courses in a couple of weeks.
Can you suggest a good PHP resource? Thanks...
jkotuby
Forum Newbie
Posts: 7
Joined: Sun Jan 17, 2010 8:37 pm

Re: Help with overcoming cross site AJAX request problem

Post by jkotuby »

Thank you suryakantb for the lengthy reply. I will definitely learn to use jQuery as it seems to be most useful.
Post Reply