...

View Full Version : Need help with JS code that gets the data out from XML



modano
03-20-2012, 06:27 AM
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/cart.aspx/tellios?report=1&xml=1

I think it should looks like this:

Mypage.htm


< 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.

Old Pedant
03-20-2012, 07:09 AM
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.

modano
03-20-2012, 07:16 AM
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.

Old Pedant
03-20-2012, 07:23 AM
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:


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 ...

webdev1958
03-20-2012, 07:31 AM
The XML data in your link is:


<?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.



<!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>

modano
03-20-2012, 07:33 AM
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?

Old Pedant
03-20-2012, 07:42 AM
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:


<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.

Old Pedant
03-20-2012, 08:05 AM
If you care, the ASP code I used for "proxy.asp" is this:


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!

webdev1958
03-20-2012, 09:32 AM
NOTE: MSIE browsers older than MSIE 7 don't support new XMLHttpRequest()....

You'll need new ActiveXObject("Microsoft.XMLHTTP") for IE6-

modano
03-20-2012, 03:29 PM
I have finally figured out with the php script and was able see the required result.
Thank you, guys, for all of your help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum