Page 1 of 1

AJAX equivalent of file_get_contents()

Posted: Wed Sep 12, 2007 11:47 am
by nwp
Is there any AJAX equivalent of file_get_contents in PHP ??
I was trying to make one but cant.

Code: Select all

function ajx_obj(){
	var xmlhttp=false;
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E){
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined'){
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

Code: Select all

function get_file(file_name){
	var tmp_str;
	var request = ajx_obj();
	request.open('GET', file_name, true);
	request.onreadystatechange = function(){
		if(request.readyState == 4){
			if(request.status == 200){
				tmp_str = request.responseText;
			}
		}
	}
	request.send(null);
	return tmp_str;
}
But this is not working.e.g. the function is not returning the text response

Posted: Wed Sep 12, 2007 11:54 am
by Luke
Umm... jquery's get() method is pretty much a file_get_contents() but to a url instead of a directory. Maybe you can glean something from it. Or just use jQuery.

http://docs.jquery.com/Ajax/jQuery.get#urldatacallback

Posted: Wed Sep 12, 2007 1:11 pm
by nwp
The Ninja Space Goat wrote:Umm... jquery's get() method is pretty much a file_get_contents() but to a url instead of a directory. Maybe you can glean something from it. Or just use jQuery.

http://docs.jquery.com/Ajax/jQuery.get#urldatacallback
But How can I know how does it work ?
is there any live example of it ?