Basic AJAX question.

J

JDeats

Note, I am not trying to use the Microsoft AJAX Framework, yes I am
aware of what it is and what it does.... The techniques refered to as
"AJAX" predate this toolkit and ASP.NET 2.0

What I am trying to accomplish is the most minimal cross-platform web
browser "AJAX" implementation using .NET 2.0, here is what I have so
far in the client and the server.

When the user clicks in Internet Explorer this does exactly what I
would expect, I get a JavaScript alert box with the data I pass in to
SendIt.aspx saying JAMES1.

So the problem is this doesn't work from other web browsers, I tried
with Safari for Windows as well as FireFox 2.0 and the alert box never
appears, it seems the XMLHttpRequest object does not work in non-IE
browsers.


CLIENT (this page has an .HTML extenshion on the web server)

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<script language=javascript>
function submitForm()
{
var xhr;
try { xhr = new XMLHttpRequest(); }
catch(e)
{
xhr = new ActiveXObject(Microsoft.XMLHTTP);
}

xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
{

var x1 = xhr.responseText;
alert(x1);
}
else
{
alert("failed");
}

}
};

xhr.open("POST", "http://localhost:2160/WebSite1/SendIt.aspx",
true);
xhr.send("JAMES1");
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<button id="Button1" value="start2"
onclick="submitForm();">Test2</button>
</div>
</form>
</body>
</html>


SERVER (here is the Page_Load event from the ASPX page)

protected void Page_Load(object sender, EventArgs e)
{
string txt = "";
string input = null;
StreamReader sr = new StreamReader(Request.InputStream);
while ((input = sr.ReadLine()) != null)
{
txt += input;
input = null;
}

sr.Close();
sr.Dispose();

//Request.SaveAs(@"c:\temp2\request.txt", true);
Response.Write(txt);
Response.End();

}
 
B

bruce barker

i don't see an error in your code. try:

xhr.open("POST", "SendIt.aspx",true);

in case the port is wrong. instead of try/catch you can also use:

var xhr = window.XMLHttpRequest ?
new XMLHttpRequest()
: new ActiveXObject(Microsoft.XMLHTTP);

-- bruce (sqlwork.com)
 
J

JDeats

i don't see an error in your code. try:

xhr.open("POST", "SendIt.aspx",true);

in case the port is wrong. instead of try/catch you can also use:

var xhr = window.XMLHttpRequest ?
new XMLHttpRequest()
: new ActiveXObject(Microsoft.XMLHTTP);

-- bruce (sqlwork.com)

Thanks. I applied those suggestions and the same behavior continues.
To be more specific:

in Internet Explorer 6 & 7 everything IS working as expected.
In Mozilla FireFox 2.0 the onreadystatechanged event is fired,
readyState is = 4, but if I try to examine the value of status an
exception is thrown, no content is returned from SendIt.aspx
In Apple Safari for Windows, onreadystatechanged event never fires.

Another thing to note: if I uncomment out the line in the SendIt.aspx
that saves the Request body to a file I notice that in both Safari and
FireFox 2.0 the request never makes it through.

All advice is welcome.
 
J

JDeats

I'm wondering if this has something to do with cross-domain
restrictions on HttpXMLRequest found in Safari and FireFox, but
aparently relaxed a bit in IE. Seems like those browsers would resolve
localhost to the same domain.

I've also tried modifying my host file to ref: www.mytestingground.com
127.0.0.1

Still the same problem there.
 

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

Top