Member Avatar for charliesindex

Hi,

I am quite new to web development. I wrote this script to display current weather it works fine locally. But not able to run on my web server - I get "Access Denied error". Can anyone help? or suggest a better way to get yahoo weather on my web site?

<html>
	<head>
	</head>
	<body>
		<table border=1><tr>
		<td width="300">
Chalaka Salpadoru
		</td>
		<td width="300">
			<SCRIPT type=text/javascript>
				xmlHttp=GetXmlHttpObject()
				if (xmlHttp==null){
					document.write("Browser does not support HTTP Request");
					//	return;
				}
				var url="http://weather.yahooapis.com/forecastrss?p=NZXX0049&u=c"
				xmlHttp.open("POST",url,false)
				xmlHttp.send(null)
				response = xmlHttp.responseXML;
				hellooo = response.getElementsByTagName("description")[1].childNodes[0].nodeValue
				document.write("<b>Current Weather in Wellington</b><br>");
				document.write(hellooo)
				function GetXmlHttpObject(){
					var xmlHttp=null;
				  	try
					{
					    	// Firefox, Opera 8.0+, Safari
					    	xmlHttp=new XMLHttpRequest();
					}
				  	catch (e)
					{
					    // Internet Explorer
					    try
					      {
						xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
					      }
					catch (e)
					{
					      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
				}
				return xmlHttp;
				}
			</SCRIPT>
		</td>
		</tr></table>
	</body>
</html>

Thank you
Chalaka

Recommended Answers

All 3 Replies

Hi, charlie

AJAX is only capable of handling files within the same domain, so if we are posting/getting request from another source(domain), then it will just dump the whole request sent by your host.
This request is not allowed to access any of those files within url process due to some security reasons. However you can use some iframes to spawn the site.

Member Avatar for charliesindex


Hi Essential, Thanks for that. Would you be able to suggest as approach to solve the problem? Thank you

Hi charlie,

here's a simple workaround that deals on this issue. You can easily strip down the content of iframe source by evaluating different statement that will filter the iframe content.
And when we are done, then we will simply override the parentElement structure and make the iframe vanished.
This can be done in many ways, but here's the simplest implementation that i can provide:

MainPage.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html dir="ltr" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Window-target" content="_top">
<title>Free Live Help!</title>
<style type="text/css" media="screen">
<!--
-->
</style>
<script type="text/javascript">
<!--
var IframeElements = { };
var firefox = Boolean((( !!document.getElementById === true ) ? 1 : 0 ));

IframeElements.append = function( thisElementId ) {
var container;
var obj;
var count = 0;
var htmlDoc;
var iframes = [ ];
var elementsToBeCreated = [ ];
var iframe = (( window.frameElement ) ? frameElement : frames );
   if ( firefox ) {
      container = document.getElementById( thisElementId ); 
      for ( var x = 0; x < iframe.length; ++x ) {
         iframes[ x ] =  document.getElementById( String( iframe[ x ].name ));
         with( iframes[ x ] ) { 
            for ( var y = Number((( document.body.sourceIndex ) + 1 )), node = document.getElementsByTagName("*"); y < node.length; y++ ) { 
            elementsToBeCreated[ count ] = document.createElement( String( node[ y ].nodeName ));
            elementsToBeCreated[ count ].appendChild( document.createTextNode( (( count >= 1 ) ? String( node[ y ].innerText ) : "" )));
            elementsToBeCreated[ 0 ].appendChild((( count === 1 ) ? elementsToBeCreated[ 1 ] : elementsToBeCreated[ 2 ] ));             style.display = "none";
  ++count; } 
         }     
      } htmlDoc =  elementsToBeCreated[ 0 ];
   } else { /* These demo has not been formalized yet, to handle IE5- Mode browsers, for the meantime try using a higher version of Web browser's like (OPERA8+/FIREFOX1+/IE7+ etc.).
The purpose of this demonstration, is to help you understand -- on how you will be able to strip elements from the iframe source. */
      alert( "Sorry I won't be able to run this demo on this browser, try Firefox instead!" );
      return false;   
   } try {
     container.appendChild( htmlDoc );
   } catch( error ) {
      container.innerHTML = htmlDoc.outerHTML;
   } return;
}; window.onload = function() {
IframeElements.append("main"); // Specify the element id in where the content of the iframe will be showed.

  // Destroy the iframe structure by overriding the content of its parent element, which is the main <div> that comes on this demo.
};
// JavaScript v1.5 -->
</script>
</head>
<body>
<div id="main"><iframe id="frm" name="frm" src="iframe.html"></iframe></div>
</body>
</html>

As you can see, this page has nothing on it, asside from the two tags, which is the <div...><iframe...></iframe></div> .
But as soon as the page done loading, it will mimic its content from the iframe source.

Note : that you must filter elements only from the body document, make sure that the header elements is washed away, or the parent page will get crappy results.

Here the content of the iframe source iframe.html:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<?xml-stylesheet type="text/css" href="#css_level21" media="screen"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="xhtml10S" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta http-equiv="Window-Target" content="_top" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>— AJAX Twitter —</title>
<style id="css_level21" type="text/css" media="screen">
/* <![CDATA[ */


/* ]]> */
</style>
<script id="javascript_v15" type="text/javascript">
// <![CDATA[

// ]]>
</script>
</head>
<body>
<div id="main"><h1>Iframe Page</h1>
<p>These Content is gathered from the iframe source.</p>
</div>
</body>
</html>

you can also filter all the elements attributes that you need.

I hope with my example, i can add new blend in your programming skills.

essential

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.