Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Some help? AJAX

  • 08-03-2010 11:35AM
    #1
    Registered Users, Registered Users 2 Posts: 2,110 ✭✭✭


    Anyone see why this wont update the value beyond 222. If so why not?

    function xGetElementById(e)
    {
    if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
    }
    return e;
    }
    var my_embedded_data_1 = 222;
    var my_embedded_data_2 = 333;

    var XHR = new xHttpRequest();


    function onResponse(req, status, obj)
    {




    try {
    eval(req.responseText);

    }
    catch(e) {
    }

    xGetElementById('my_html_data').innerHTML = my_embedded_data_1;

    setTimeout("issueRequest()", 2000);
    }

    function issueRequest()
    {
    XHR.send('GET', 'd.stm', 't=' + new Date().getTime(), 7000, false, false, null, onResponse);
    }

    window.onload = function()
    {
    issueRequest();
    }
    </script>
    </head>


    <div id="my_html_data"><div>


Comments

  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    Um, silly question as my javascript-fu is weak, but where do you ever touch the value other than in the initial decleration in onResponse()? I don't see any increment operations there at all.


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    xGetElementById('my_html_data').innerHTML = my_embedded_data_1;
    

    You only ever look for the value 222 (i.e. my_embedded_data_1) so you'll only ever get back 222.

    What exactly is it that you want this code to do and maybe we can point you in the right direction?


  • Registered Users, Registered Users 2 Posts: 2,110 ✭✭✭sei046


    Hey guys, ye I figured that!

    So basically on "d.stm" There is a value which changes everytime the page is opened.
    When I go to "sensors.stm" I want a div on it to display the value on "d.stm" every second. Seems simple but cant get it to work!


  • Registered Users, Registered Users 2 Posts: 339 ✭✭duffman85


    Is xHttpRequest() a wrapper for XMLHttpRequest? It's hard to see what everything is doing without the rest of your code.

    How is the onResponse function receiving those parameters - req,status,obj?

    all that needs to be in your onResponse function is to check that the request has been successfull(Status=200) and that it has completed(readystate=4)

    Then you can write the following in your onResponse function
    var response = XHR.responseText;
    xGetElementById('my_html_data').innerHTML = response;
    

    W3Schools -AJAX ResponseXML is worth a look.


Advertisement