SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to correct getting xmlhttp.readyState = 0

    Hello,

    In a AJAX code I have, I am getting xmlhttp.readyState = 0 rather than 4 which of course
    is the correct result.

    I use the same AJAX code in other pages and it always returns the correct state of 4.
    But somehow on this new page it keeps returning 0.
    I have put all sort of debugging code and cannot see why it is doing that!

    What suggestions do you have to fix mlhttp.readyState = 0?

    BTW, you can see the page here:
    http://www.anoox.com/ask_answer/qanda.php?q_id=132453

    And the item in question that is firing the AJAX code is when user select from the
    "Report As" selection drop down list in front of the "Answer This Question" button.
    As you can see it has debug alert code associated with it currently.

    ThanX,

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    I use the same AJAX code in other pages and it always returns the correct state
    Code:
    xmlhttp.onreadystatechange=stateChanged();
    I doubt that your working code has a line like that.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    You are actually right. In the other code it is like:
    xmlhttp.onreadystatechange=stateChanged;

    so there are not () after stateChanged.

    But since stateChanged is a function, that is:

    function stateChanged()
    {

    if (xmlhttp.readyState == 4) {

    var result = xmlhttp.responseText;
    if (result == 2) {
    document.getElementById('report_div').style.display = 'block';
    document.getElementById('report_div').innerHTML = 'OK';
    }
    }

    I thought I could include the () after stateChanged so that I can pass then variables to it from function that is called from the Web page.
    Put another way I need to pass variable to stateChanged so that it knows which DIV to update. So the value of report_div as is currently hard coded in stateChanged needs to be given to it. How can one do this?

    Regards,


    Quote Originally Posted by Logic Ali View Post
    I doubt that your working code has a line like that.

    Anoox search engine volunteer

    www.anoox.com

  4. #4
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Just wanted to report to you that I found a great way to pass the DIV value from Web page to AJAX function for use later by function stateChanged().
    I got the job done by setting the DIV value as a global variable which is then retrieved by function stateChanged().
    FYI, using these 2 function:

    function setValue(div_val)
    {
    window.myValue = div_val;
    }

    function getValue()
    {
    return window.myValue;
    }


    Sweet

    Anoox search engine volunteer

    www.anoox.com

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    function setValue(div_val)
    {
    window.myValue = div_val;
    }

    function getValue()
    {
    return window.myValue;
    }


    Sweet
    as a lemon

    Code:
    xmlhttp.onreadystatechange = function(){ stateChanged( div_val ); }
    Tab-indentation is a crime against humanity.

  6. #6
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,033
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Actually I got thinking that would this method I have found for saving values between 2 JS functions work OK in
    case of Apple and other non-Windows based devices?
    Since the method is based on:

    window.myValues = [report, rpt_for, qid, aid, div_name, votes];

    and that window. maybe working on Windows based PCs only!

    Do you know?

    ThanX,

    Quote Originally Posted by Logic Ali View Post
    as a lemon
    Code:
    xmlhttp.onreadystatechange = function(){ stateChanged( div_val ); }

    Anoox search engine volunteer

    www.anoox.com

  7. #7
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WorldNews View Post
    Actually I got thinking that would this method I have found for saving values between 2 JS functions work OK in
    case of Apple and other non-Windows based devices?
    Yes it works on all platforms, however ideally you should try to get away from using global variables.
    Is there any reason that your readystatechange handler can't run the statements necessary to get the required value?
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •