Jump to content

[SOLVED] Script returns: undefined


kartul

Recommended Posts

i'm new to ajax. i've made a script that should return results from database but it keeps telling me just undefined. i googled and i know what the problem is, I just can't find it. i've stared the code for now about 15 minutes but I just can't see the problem here.

 

var ajax = null;

function showRes(wut) {
	if(wut.length == 0) {
		document.getElementById("results").innerHTML = "";
		return;
	}
	if(window.XMLHttpRequest) {
		// IE7+, ff, chrome, opera, safari
		ajax = new XMLHttpRequest();
	}else if(window.ActiveXObject) {
		// retards
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}else {
		alert("Your \"browser\" does not support AJAX!");
		return;
	}

	var url = "search.php?q=" + wut;
	url = url + "&sid=" + Math.random();
	ajax.open("GET", url, false);
	ajax.send(null);
	document.getElementById("results").innerHTML = ajax.reponseText;
}

Link to comment
Share on other sites

Errrrr....  You need to read an AJAX tutorial >.<.

 

 

When using AJAX asynchronously (how it's almost always used), you must use a callback function.

yea but... but this script works.

var xmlhttp = null;

function showRes(str) {
	if (str.length == 0) { 
		document.getElementById("results").innerHTML="";
		return;
  		}
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	}else if (window.ActiveXObject) {
		// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}else {
		alert("Your browser does not support XMLHTTP!");
		return;
	}

	var url = "search.php?q=" + str;
	url = url + "&sid=" + Math.random();
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	document.getElementById("results").innerHTML = xmlhttp.responseText;
}

and they are almost identical, just one doesn't work.

 

i got them from w3schools. so they teach it wrong?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.