Jump to content

ajax retreive help


dzhax

Recommended Posts

I am using ajax to develop a tool for use at work. The main problem is when i try to retrieve a remote php file it stops working.If it is local then it works fine.1. Is it possible to get a remote file?2. If so how?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">  <head>	<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />	<script type="text/javascript">	  var bustcachevar=0 //bust potential caching of external pages after initial request? (1=yes, 0=no)	  var loadedobjects=""	  var rootdomain="http://www.remote-location.com/location_of_php_file/"	  var bustcacheparameter=""	  function ajaxpage(url, containerid){		var page_request = false		if (window.XMLHttpRequest) // if Mozilla, Safari etc		  page_request = new XMLHttpRequest()		else if (window.ActiveXObject){ // if IE		  try { page_request = new ActiveXObject("Msxml2.XMLHTTP") } 		  catch (e){		  try{ page_request = new ActiveXObject("Microsoft.XMLHTTP") }		  catch (e){}		}	  } else		return false		page_request.onreadystatechange=function(){		  loadpage(page_request, containerid)	  }			if (bustcachevar) //if bust caching of external page		  bustcacheparameter=(rootdomain+url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()		  page_request.open('GET', rootdomain+url, true)		  page_request.send(null)		}		function loadpage(page_request, containerid){		  if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))			document.getElementById(containerid).innerHTML=page_request.responseText		  }		  function loadobjs(){			if (!document.getElementById)			  return			for (i=0; i<arguments.length; i++){			  var file=arguments[i]			  var fileref=""			  if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding			  if (file.indexOf(".js")!=-1){ //If object is a js file				fileref=document.createElement('script')				fileref.setAttribute("type","text/javascript");				fileref.setAttribute("src", file);			  }			  else if (file.indexOf(".css")!=-1){ //If object is a css file				fileref=document.createElement("link")				fileref.setAttribute("rel", "stylesheet");				fileref.setAttribute("type", "text/css");				fileref.setAttribute("href", file);			  }			}			if (fileref!=""){			  document.getElementsByTagName("head").item(0).appendChild(fileref)			  loadedobjects+=file+" " //Remember this object as being already added to page			}		  }		}	</script>  </head>  <body onload="java script:ajaxpage('getDashboard.php', 'contentArea');">	<div id="contentArea" class="content">	</div>  </body></html>

Link to comment
Share on other sites

For security reasons, that is not possible - you will need to make a local server-side script that can proxy the remote file and make it available to the JavaScript client.

Link to comment
Share on other sites

ugh... the limitations of not having php on the server the webpage is on.anyone else know a way i could "include" a remote php file into a webpage that is not on a real webserver.The way it will be accessed is via a network folder that employees will click on via a shortcut.I was planning on hosting the php and database on my webserver to make it easier for me to manage it. But it will never get past upper management if i had to host the whole site on a publicly accessible web server.----------------------------EDIT: The way I currently have it setup is all html and css hardcoded to an html file on a file server. I know its not the ideal setup for advanced configurations that i am trying to pull off but it would make things a lot easier.

Link to comment
Share on other sites

atm not that i am aware of. like i said it is just a network folder that everyone on the team has access too. Im working with a supervisor to get some type of a webserver but just in case i want to be prepared for the worse.synook mentioned something about a proxy, would that require extra software or is this something i can implement on the actual remote server to provide the needed content to the webpage?

Link to comment
Share on other sites

You can't implement the proxy on the remote server, because then your JS code would still need to access that server. The point of a local proxy is to bring the content into reach of the JavaScript script.

+-----------------------+| Local domain		  |+--------+--------------+| Client | Server	   ||		|			  || +--+   |			  |			  +--------+| |JS|----------------->|Blocked	   | Remote || +--+   |			  |			  +--------+|		|			  |				  ^| +--+   | +-----+	  |				  || |JS|---->|Proxy|-------Not blocked-------+| +--+   | +-----+	  ||		|			  |+--------+--------------+

Nevertheless, if you have full access to the remote server there are other ways to get around the AJAX single-domain limitation, such as JSONP or even XMLHttpRequest Level 2 (though the latter is not widely implemented yet).

Link to comment
Share on other sites

You guys have to have an old PC hanging around, right? You should be able to build a lightweight server of your own with little trouble and no expense, and you can still keep it behind your firewall, if that's the issue.

Link to comment
Share on other sites

I work for a rather large corporation. And basically we are trying to work around conditions that divisional gives us. This project is to help newer team members with links to tools and training to aid them. I have it hard coded but it would make it so much easier if i could make a small cms with a database to handle the repetitive code.I know html and i am familiar with php. When it comes to ajax i am limited to the tutorials i can find online. As far as anything else not sure i would have to research and try to learn it.that XMLHttpRequest Level 2 looks promising just not 100% on how to use it.

<script type="text/javascript">var url = "http://foo.bar/getInfo.php"if(XMLHttpRequest){  var request = new XMLHttpRequest();  if("withCredentials" in request)  {   // Firefox 3.5 and Safari 4   request.open('GET', url, true);   request.onreadystatechange = handler;   request.send();  }  else if (XDomainRequest)  {   // IE8   var xDr = new XDomainRequest();   xDr.open("get", url);   xDr.send();    // handle xDR responses -- not shown here :-)  }  // This version of XHR does not support CORS   // Handle accordingly}</script>

that is the sample code i found to work with.How can i get the printout of getInfo.php and place it into <div id=content></div>I think im just missing a line or 2 of code, but i cant seem to figure it out. And like synook explained its not highly used to resources are limited.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...