XMLHttpRequest running a ASP.net Script

K

K

Hello there, this is my first time using these google groups..so if i'm
doing somethign wrong or in the wrong place let me know

What i'm trying to do is use the XMLHttpRequest object to send a string
to a custom script i made, and then depending on what the string
is..have the script do some work and return a value.

I'm using javascript for the XMLHttpRequest object, and sending the GET
Request to a ASP.net script (i think the problem lies in my
script)...but heres the code I guess

Here is my basic .htm javascript page
<script type="text/javascript">
var HttpObject;
function SendRequest(url)
{
HttpObject = getHttpObject();
HttpObject.open("GET", url, true);
HttpObject.onreadystatechange = getHttpResponse();
HttpObject.send(null);
}

function getHttpResponse()
{
if (HttpObject.readyState == 4)
{
alert(HttpObject.responseText);
}
else
{
alert(HttpObject.readyState);
}
}
function getHttpObject()
{
// gets the object fine so i took this part out
}
</script>

Here is where i think the trouble is..I didn't know how to make just a
stand alone asp.net script so i just made like a webpage..take a look

<ASP.NET SCRIPT PAGE>
<%@ Page Language="C#" Debug="true" %>
<%@Import namespace='System.Data' %>
<%@Import namespace='System.Data.OleDb' %>
<script type="text/C#" runat=server>
public OleDbConnection cn;
public void ConnectToDatabase()
{
cn = new OleDbConnection("connection string that you guys can't see");
cn.Open();

}
public string Str;
public string JScript;
</script>
<html>
<head runat=server>
<%
Str = Request.QueryString.Get("str");
JScript = "";
ConnectToDatabase();

string sSQL = "my sql statement";

OleDbCommand Cmd = new OleDbCommand(sSQL, cn);
OleDbDataReader Reader = Cmd.ExecuteReader();
while (Reader.Read())
{
JScript += "\"" + Reader.GetString(0) + "\",";
}
JScript = JScript.Remove(JScript.Length);
JScript += ");";
cn.Close();
Response.Write(JScript);
%>

</head>
<body>
</body>
</html>
</ASP.NET SCRIPT PAGE>

The Problem is that the ReadyState is always 1..which i think means no
request was sent...but on my button handler in the html page i have an
onClick="javascript:SendRequest('scriptpage.aspx?str=foobar')";

any help would be awesome..thanks
 
D

Donius

When you do this:
HttpObject.onreadystatechange = getHttpResponse();

I think what you probably mean is this:
HttpObject.onreadystatechange = getHttpResponse;

Doing it the second way will call the function getHttpResponse on every
readystatechange. If you do it the first way, instead of assigning the
function itself, you're assigning the function *result* to
onreadystatechange. And since the result is not a function, nothing
gets called when the readystate changes. Does that make sense?

-Brendan
 
K

K

Yes it does, thank's alot..that was the problem

but now...

It returns the whole asp.net webpage (isn't a problem really because i
just parse out what i need, or don't need)

but when I load the list into the <div>..for some reason..it doesn't
update it until i click on the <div>...i'm loading it in using
javascript with the div.innerHTML...any ideas on this one?

Again, thanks a lot Donius
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top