Jump to content

Xmlhttp.open


ckrudelux

Recommended Posts

What have I missed?

function getXMLHttp(){	var xmlHttp	try	{		//Firefox, Opera 8.0+, Safari		xmlHttp = new XMLHttpRequest();	}	catch(e)	{		//Internet Explorer		try		{			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");		}		catch(e)		{			try			{				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");			}			catch(e)			{				alert("Your browser does not support AJAX!")				return false;			}		}	}	return xmlHttp;}

var xmlHttp = getXMLHttp();var postdata = "?text=hello";xmlHttp.open('GET', 'communicate.php'+postdata, true);xmlHttp.send(null);

Not sending anything :S

Link to comment
Share on other sites

Are you sure it's not sending anything? Have you checked your server logs?You don't have a callback set, so you're certainly not going to get the response if there is any.Are there are error messages in the error console? What other code do you have on the page (a parsing error may lead to this code not being executed...).

Link to comment
Share on other sites

Are you sure it's not sending anything? Have you checked your server logs?You don't have a callback set, so you're certainly not going to get the response if there is any.Are there are error messages in the error console? What other code do you have on the page (a parsing error may lead to this code not being executed...).
Server logs? callback? I'm trying take the part I need from a function have no clue what so ever whats happening couse I don't know what key elements I'm missing if I do miss any, anyway I don't know for sure if it's not sending anything but I don't get all $_GET's to my php script couse if I alert postdata with my longer line of gets and copy paste it after the php file in the browser it does do what it should so the data isn't going through. No errors from firebug.var postdata = "?submitmove&type=groups&do=add&gid=2&oid=84&type2=1&table=paragraph_groups";
Link to comment
Share on other sites

You should read W3Schools' AJAX tutorial, as well as the XMLHttpRequest() object in the XML DOM tutorial and the XMLHttpRequest() object in the XML DOM reference to understand what's going on.If you want to make sure the request gets executed, try to write your $_GET and $_POST data into files.For example:

<?phpfile_put_contents('GET.txt', print_r($_GET, true));file_put_contents('POST.txt', print_r($_POST, true));?>

After trying to execute the JavaScript code, you shold be able to open GET.txt and POST.txt, which will be placed in the same folder as your PHP script. They will contain the data your PHP received the last time it was called. If the files exists and contain the right data, it means your PHP script was activated, and therefore, that the JavaScript worked.

Link to comment
Share on other sites

You should read W3Schools' AJAX tutorial, as well as the XMLHttpRequest() object in the XML DOM tutorial and the XMLHttpRequest() object in the XML DOM reference to understand what's going on.If you want to make sure the request gets executed, try to write your $_GET and $_POST data into files.For example:
<?phpfile_put_contents('GET.txt', print_r($_GET, true));file_put_contents('POST.txt', print_r($_POST, true));?>

After trying to execute the JavaScript code, you shold be able to open GET.txt and POST.txt, which will be placed in the same folder as your PHP script. They will contain the data your PHP received the last time it was called. If the files exists and contain the right data, it means your PHP script was activated, and therefore, that the JavaScript worked.

Okay after playing around a bit I added this to my code and took away the old one:
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, SafarixmlHttp=new XMLHttpRequest();}else{// code for IE6, IE5xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}

It works but I know what the old one works cause I use it on other scripts so whats the difference they do the same but the one from the example is shorter :S

Link to comment
Share on other sites

You can actually short it down even further, while still account for browsers with no AJAX support (some mobile ones being the most notable of today's browsers) like so:

var xmlHttp= window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null);

Later, if you want to check if xmlHttp is supported, you just check if

xmlHttp != null

and if so, AJAX is supported.

Link to comment
Share on other sites

You can actually short it down even further, while still account for browsers with no AJAX support (some mobile ones being the most notable of today's browsers) like so:
var xmlHttp= window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null);

Later, if you want to check if xmlHttp is supported, you just check if

xmlHttp != null

and if so, AJAX is supported.

Still don't know why the script didn't work before cause I have only changed that code.. But it works now so I'm happy at least.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...