Help with AJAX please.?

WYSIWYG

New member
Hi, I have the below asp code. I know.... asp lol. But thats what I have to use. Ok here it is:

<%
Dim oRS, oConn
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & Server.MapPath("dbb.mdb")
oConn.Open

Set oRS = Server.CreateObject("ADODB.Recordset")
oRS.Open "select * from tblTest", oConn, 2, 3

If oRS.EOF Then
Response.Write "none"
End If


If Not oRS.EOF Then
Response.Write oRS("Title")
End If


oRS.Close
Set oRS = Nothing
oConn.Close
Set oConn = Nothing
%>


When this script is called I expect it to send back the title value in oRS. The html page is:


<html>
<title>Hi</title>
<body bgcolor="#DDDDDD">

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">

function requestNames() {

document.body.style.cursor='wait';
var oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
var sURL = "full path removed.asp"
oXMLHTTP.open( "POST", sURL, false );
oXMLHTTP.send();

if (oXMLHTTP.responseText == "none")
alert("No Values");

if (oXMLHTTP.responseText != "none")
alert(oXMLHTTP.responseText);


document.body.style.cursor='auto';
}
</SCRIPT>

<input type="submit" value="Submit" onClick="requestNames()"/>


</body>
</html>

When I run this, if no results are found the alert No Values is shown, however, if records are found the title is not shown in the alert. SOme html is shown instead inside the alert. Ahy help would be much appriciated.
 
It would be helpful to know what that "some html" you are talking about actually looks like.

At a glance, your code is actually missing the first A in AJAX. The first A stands for asynchronous which means that your code should continue to execute while the request is being process.

Now think this: what would happen in your script if the request would take a minute (or an hour) ? You call the open() function, then the request is processed (for an hour).

Your javascript moves to the next instruction which checks for a responseText. However since the request is still being processed, there is no response text (first check fails, second check is true, since the null responseText is different than "none"). So you get a HTML (probably representing the state of the request).

Normally, you must use this piece of code to treat AJAX response

oXMLHTTP.onreadystatechange=function() {
if (oXMLHTTP.readyState==4) {
if (oXMLHTTP.responseText == "none") {
alert("No Values");
} else if (oXMLHTTP.responseText != "none") {
alert(oXMLHTTP.responseText);
}
}
}

This function will be called everytime the state of the request changes (while the rest of the js will continue to run). A state of 4 means the request was complete and has returned a result.
In your particular case (when you are probably running the script on your local server) when you don't have results in the query the return is nearly instantaneous so you actually have a responseText on the next line. But otherwise when the server will spend some time processing the query results, you won't so you must rely on that readyState.
 
Back
Top