Facility Control

 View Only
  • 1.  Return from async http post

    Posted 10-30-2023 09:21

    Hi, 

    I'm looking to be able to fire a function that returns xml data from an http post request without having to save it as a parameter such as calling GetXML('http://webaddress')

    function GetXML (URL)
    {
    function callback(resultStr)
    {
       var xml = ogscript.parseXML(resultStr);
       var status = ogscript.runXPath('//state', xml);
       var state = status.item(0).getTextContent();
       return state;
    }
    ogscript.asyncPost(URL, '', callback);
    }

    Cheers,

    Dan



    ------------------------------
    Dan West
    ------------------------------


  • 2.  RE: Return from async http post

    Posted 11-01-2023 14:38

    Hi Dan,

    To double-check, to accomplish this, you'd have to have some var in the parent scope that's visible to the callback function. In the callback, it would set the var to store the XML data. After the asyncPost finishes, there would need to be some separate function called to return the var containing the XML data.

    Best Regards,



    ------------------------------
    Altaz Daruwala
    Ross Video
    ------------------------------



  • 3.  RE: Return from async http post

    Posted 11-02-2023 09:23

    Hi Dan,

    Apologies for the misinterpretation. The answer to this question is that none of the async function calls directly return a value since they are run on a different processing thread to avoid locking up the user interface.  There is a blocking call to ogscript.post but it should never be run directly from the user interface thread (e.g. triggered by a button click) as it will make the entire UI pause while it fetches the information from the web server – it needs to be run from ogscript.asyncExec like this:

    ogscript.asyncExec(function()
    
    {
     var resultStr = ogscript.post(URL, '');
     var xml = ogscript.parseXML(resultStr);
     var status = ogscript.runXPath('//state', xml);
     var state = status.item(0).getTextContent();
     updateState(state); //You would still want something like this to handle your update
    });

    Best Regards,



    ------------------------------
    Altaz Daruwala
    Ross Video
    ------------------------------