|
-
Dec 28th, 2009, 07:12 PM
#1
Thread Starter
Addicted Member
Can't get responseText from an ASP.NET page using XMLHttpRequest in JavaScript
Hi, no browsers (apart from Safari) can seem to get responseText from an ASP page on the ASP.NET Development Server app that comes with Web Developer.
Here's my client side JavaScript code used to get a response:
Code:
var req = new XMLHttpRequest();
req.onreadystatechange = function()
{
alert(req.responseText);
}
req.open("GET", "http://localhost:50819/Test/login.aspx?username=testuser&password=password", true);
req.send(null);
(I am fully aware that this will not work in IE because I haven't implemented the ActiveX method for this).
I am using this C# code to send a response:
Code:
Response.ContentType = "text/plain";
Response.ContentEncoding = Encoding.UTF8;
Response.Write("L94_CORRECTLOGININFO");
No HTML is in the response, just the "L94_CORRECTLOGININFO" message.
Any ideas on this would be great because I don't have a clue what's going wrong!
Louix
-
Dec 29th, 2009, 05:46 AM
#2
Re: Can't get responseText from an ASP.NET page using XMLHttpRequest in JavaScript
Try modifying the callback handler to:
Code:
req.onreadystatechange = function()
{
if (req.readyState == 4) {
if (req.status == 200) {
alert(req.responseText);
}
}
}
IIRC, the onreadystatechange gets called multiple times as its status changes.
-
Dec 29th, 2009, 01:32 PM
#3
Re: Can't get responseText from an ASP.NET page using XMLHttpRequest in JavaScript
Hey,
You can find some more information about this here:
http://www.w3schools.com/ajax/ajax_xmlhttprequest.asp
Gary
-
Dec 31st, 2009, 03:02 PM
#4
Thread Starter
Addicted Member
Re: Can't get responseText from an ASP.NET page using XMLHttpRequest in JavaScript
Thank you for your replies, sorry this is a bit overdue but I had to go away for the past few days.
I've got the JavaScript side down alright, but thank you for your code mendhak because it helped me find out that the status never reaches 200 OK.
Is there something I'm missing?
Thank you
-
Jan 2nd, 2010, 05:01 AM
#5
Re: Can't get responseText from an ASP.NET page using XMLHttpRequest in JavaScript
Try browsing straight to the URL in all of your browsers.
http://localhost:50819/Test/login.as...sword=password
Do you get what you expect? If you're getting what you expect, then the javascript should work. You may also want to inspect the actual status coming back. If it's not 200, what is it?
-
Jan 4th, 2010, 09:00 PM
#6
Thread Starter
Addicted Member
Re: Can't get responseText from an ASP.NET page using XMLHttpRequest in JavaScript
I get a response with status 200 if I browse to it, so there appears to be nothing wrong with my server code either.
-
Jan 6th, 2010, 10:55 AM
#7
Re: Can't get responseText from an ASP.NET page using XMLHttpRequest in JavaScript
OK, so what is the status that you do get back in JS? Do this
Code:
req.onreadystatechange = function()
{ alert(req.status);
if (req.readyState == 4) {
if (req.status == 200) {
alert(req.responseText);
}
}
}
You should get multiple alerts as the status code changes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|