How to get XML data from external server

Discussion in 'JavaScript' started by big_red, Dec 14, 2007.

  1. #1
    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>
     
    big_red, Dec 14, 2007 IP
  2. akram

    akram Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    I did not test your code but if you know your code is correct but due to the cross domain issue you are not able to get the Ajax to work... then you can do the following to simply solve the problem.

    Send the Ajax request to a PHP script on your server which can request to the main XML source and pass the result back to your Ajax function.


    If you re not clear about what i'm saying or not able to write such PHP script, tell me i can help you in writing it.

    Regards,
    Akram
     
    akram, Dec 16, 2007 IP
  3. big_red

    big_red Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Unfortunately, I don't have the experience to write the PHP as you are describing. I have done a little before, but it's been pretty basic. If it's not too difficult, I'd appreciate any help you can give.

    Thanks
     
    big_red, Dec 17, 2007 IP
  4. akram

    akram Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,

    try this.

    
    <?
    include "http://le.utah.gov/GIS/GISLNK?function=lookup&address=$_GET[addressVal]&city=$_GET[cityVal]&zip=$_GET[zipVal]";
    ?>
    
    PHP:
    change the URL in your JavaScript to the above php script but still send the parameters threw GET.

    Hope this works!

    Let me know the result.

    Regards,
    Akram
     
    akram, Dec 19, 2007 IP
  5. big_red

    big_red Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the help. I tried doing what you said, and I couldn't get it to work, although it gave me a lot of different ideas to research. The closest I was able to get was to hard code the url into the php file and call it from the js in the html file. But of course, hard coding the url defeats the purpose, although it proved that this method could potentially work.

    I think what's happening is that the $_GET values are not being received by the php file. Do I need to have anything else going on in the html/js file to make sure they get over?

    I even tried something I found in another post, where you create a call to the php file something like

    localhost/phpfile.php?paramname=paramvalue

    where paramvalue is a variable from the js, and paramname is the name.

    Then in the php file, it should be something like
    $param = $_GET['paramname'];
    include "http://le.utah.gov/GIS/GISLNK?function=lookup&" . $param;

    So far, nothing has worked though, other than explicitly hard coding the entire url.

    Also, as another note, if I remove the hard coding from the php file, and I type in my browser
    http://mysite.com/myphpfile.php?param=address=100%20S%20Main%20St&city=Salt%20Lake%20City&zip=84101
    (which is really just hardcoding the address in the browser rather than the php file), that also works. Just another indication that maybe the php file is set up correctly, and the issue may lie with the js/html file.


    Thanks
     
    big_red, Dec 20, 2007 IP
  6. arnek

    arnek Active Member

    Messages:
    134
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    What I'll do which is maybe not the same you wanna do is , perform the standard fopen(file or URL) command(PHP),
    and then read the reasults into a variable.

    http://www.php.net/manual/en/function.fopen.php

    you can now parse the returned values with your favourite xml parser.

    OR

    Would it be able to use web services, there are quite a few nice Web Service libraries online to consume or provide web services
     
    arnek, Dec 20, 2007 IP
  7. pratham

    pratham Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Are you saying that php is not reading your GET params?

    Perhaps you might have done this, but you can check all the passed GET params using
    print_r ($_GET);
    PHP:
    The fact that it works when you enter the URL in the browser would suggest that something is wrong at the Javascript end.
     
    pratham, Dec 20, 2007 IP