Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Need help with JS code that gets the data out from XML

    Hello,

    Im new to JavaScript and this forum, right now im looking for a simple JS script, that will get the data out from the url and print it on my webpage:
    http://www.asecurecart.net/server/ca...report=1&xml=1

    I think it should looks like this:

    Mypage.htm
    Code:
    < Some JavaScript Code Im looking for>
    <html>
    Your shopping cart:
    <br>
    Items:
    <script type="text/JavaScript"><!--document.write(item_total)//-->
    </script>
    <br>
    Order Total:
    <script type="text/JavaScript"><!--document.write(order_total)//-->
    </script>
    </html>
    Any help would be very appreciated. Thanks.
    Last edited by modano; 03-20-2012 at 06:30 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Ummm...you can't. Not unless your web page is ALSO on the site http://www.asecurecart.net

    JavaScript doesn't allow you to read from a site other than the one the web page came from.

    To do this, you would need a server-side proxy on your own site that would read the file from that site and echo it back out for your JavaScript to read. But if you need a server-side proxy, you might well be better off then simply doing the XML processing server-side.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    modano (03-20-2012)

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yeah, my bad, i should have knew it before posting that (.
    But thanks anyway for an answer, will try some other solutions to solve my problem.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    A proxy server isn't hard to create in any server side language: PHP, ASP, JSP, etc.

    say your created a PHP page that took the query string passed to it and used that as the URL for it to go fetch. Then it would just echo back 100% of what it got.

    Your JS code would be as simple as something starting with this:
    Code:
    var xml = new XMLHttpRequest();
    xml.open( "GET","proxy.php?www.asecurecart.net/server/cart.aspx/tellios?report=1&xml=1",false);
    ... then use xml DOM methods to extract your data from the response ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    The XML data in your link is:

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    - <cart> 
       <item_total>0</item_total> 
       <qty_total>0</qty_total> 
       <order_total>$0.00</order_total> 
    </cart>
    This gets the xml data in your link in my IE9 but I have to change its security settings, so it's not really a practical solution but it does show you can access the xml file.

    Code:
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
            <title></title>
            <style type="text/css"></style>
            <script type="text/javascript">
    
            </script>
        </head>
        <body>
            <script type="text/javascript">
                if (window.XMLHttpRequest) {
                    xmlhttp=new XMLHttpRequest();
                }else{
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.open("GET","http://www.asecurecart.net/server/cart.aspx/tellios?report=1&xml=1",false);
                xmlhttp.send();
                xmlDoc=xmlhttp.responseXML;
                var cartO = xmlDoc.getElementsByTagName('cart');
                alert(cartO.length);  //outputs  1
            </script>
        </body>
    </html>
    Last edited by webdev1958; 03-20-2012 at 09:34 AM.

  • Users who have thanked webdev1958 for this post:

    modano (03-20-2012)

  • #6
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OK, got it, but what if i use html language for most of my pages, and not the PHP, will it still works for me?

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Sure.

    Let's assume you indeed have such a PHP page, that simply takes the URL given in the query string, goes and grabs the data from that page/site/url, and echoes it out.

    So your HTML page could be as simple as something like this:
    Code:
    <html>
    <body>
    <script type="text/javascript">
    var xmlget = new XMLHttpRequest();
    xmlget.open( "GET","proxy.php?www.asecurecart.net/server/cart.aspx/tellios?report=1&xml=1",false);
    try {
        xmlget.send();
        var xml = xmlget.responseXML.documentElement;
        var itemtotal = xml.getElementsByTagName("item_total")[0].firstChild.nodeValue;
        var qtytotal = xml.getElementsByTagName("qty_total")[0].firstChild.nodeValue;
        document.write( "Item total: " + itemtotal + "<br/>Quantity total: " + qtytotal);
    } catch ( ex ) {
        document.write( ex.message );
    }
    </script>
    </body>
    </html>
    (I just tried it, though I used an ASP proxy instead of a PHP proxy, and it worked like a charm.)

    NOTE: MSIE browsers older than MSIE 7 don't support new XMLHttpRequest(), if you care at all.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    If you care, the ASP code I used for "proxy.asp" is this:
    Code:
    url = Request.QueryString
    Set http = Server.CreateObject("msxml2.ServerXMLHTTP")
    http.Open "GET", "http://" & url, False
    http.Send
    Response.ContentType = "text/XML"
    Response.Write http.responseText
    Response.End
    %>
    Pretty simple, no? I think it can be a line or two shorter in PHP. Don't forget to set the content type!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    modano (03-20-2012)

  • #9
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Old Pedant View Post
    NOTE: MSIE browsers older than MSIE 7 don't support new XMLHttpRequest()....
    You'll need new ActiveXObject("Microsoft.XMLHTTP") for IE6-

  • #10
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I have finally figured out with the php script and was able see the required result.
    Thank you, guys, for all of your help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •