Results 1 to 7 of 7

Thread: Can't get responseText from an ASP.NET page using XMLHttpRequest in JavaScript

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    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.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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
  •  



Click Here to Expand Forum to Full Width