LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

displaying variables with xmlhttprequest and "get"

Solved!
Go to solution

I am attempting to read and write shared variables from a web browser using LV 8.6 web services.  I have been able to successfully write the variables using the code below, but when i call the responseAjax function the readyState comes back = 1 and the responseText comes back empty.  request.status does not come back at all.  The stored variables are being sent to the downUrl (see below) as plain text and i can see them there.  How do i get the variables from the output url and into my main page?

 

<script language="JavaScript" type="text/javascript">

function getXMLHttpRequest()    {
    var request = false;
    try    {
        request = new XMLHttpRequest();
    }
    catch(err1)    {
        try    {
            request = new ActiveXObject("Msxm12.XMLHTTP");
        }
        catch(err2)    {
            try    {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(err3)    {
                request = false;
            }
        }
    }
    return request;
}

var request = new XMLHttpRequest();


function upload(name, value)    {
    var upUrl = "http://localhost/mywebservice/" + name + "/" + value;
    request.open("GET", upUrl, true);
    request.send(null);
}

function download()    {
    var downUrl = "http://localhost/mywebservice/output";
    request.open("GET", downUrl, true);
    request.onreadystatechange = responseAjax();
    request.send(null);
}

function responseAjax()    {
alert(request.readyState);
alert(request.status);
alert(request.responseText);
var data = request.responseText;
document.getElementById("textout").innerHTML = data;
}
 

Html code that i didn't bother to include simply calls the upload or download functions on some event.

0 Kudos
Message 1 of 7
(6,272 Views)

Hi Pojo,

 

Welcome to the forums! One reason you might not be getting a response is bad URL mappings, be sure to check that. Also, could you post both your full HMTL code that contains the script and your LabVIEW project in a .zip file? Thanks

Joshua B.
National Instruments
0 Kudos
Message 2 of 7
(6,236 Views)

Attached is the full project and html code.  Once the project is open run the "transfer" vi and deploy the webservice.  At the page http://localhost/mywebservice/webpage/simpleCAS.html you have a couple of radio buttons that can be selected.  When these buttons are selected the variables displayed in the "transfer" vi are updated, proving that the data is reaching the vi.  If you go the http://localhost/mywebservice/output you can see the correct output is there as well.  However when you press the "Get variables" button on the input page the variables are not retrieved, and a status code is not available.  Thanks for any suggestions.

 

0 Kudos
Message 3 of 7
(6,232 Views)
Solution
Accepted by Pojoaque40

Hi,

 

It looks like you have a slight bug where you set up your callback:

 

function download()    {
    var downUrl = "http://localhost/mywebservice/output";
    request.open("GET", downUrl, true);
    request.onreadystatechange = responseAjax(); //<- drop the parentheses

    request.send(null);
}

 

As coded, this will call the responseAjax function immediately, and then set request.onreadystatechange to null, so your XMLHttpRequest object won't be able to fire the callback.  If you drop the parantheses, it will load a reference to your callback function.  Also, within the callback itself, it is best to check for readyState == 4 and status == 200.  Anything else indicates either the request hasn't completed, or there was an error in the transfer.  At any other state, the responseText may not have been completely transferred.

 

I hope that helps!

 

Regards,

Jeremy_B

Applications Engineer
National Instruments
Message 4 of 7
(6,213 Views)
Thank you - this was the solution!  One other question though.  Now that i am successfully moving variables back and forth, what is the best way to automatically update variables output to the webpage?  Should i have a javascript loop that runs the download function every so often or is there a better way?  Thanks again!
0 Kudos
Message 5 of 7
(6,200 Views)

With the code you currently have it is easy enough to add a loop to run the download function on a timer. Check out Javascript setTimeout() here http://www.w3schools.com/js/js_timing.asp.

Message Edited by Joshua B. on 12-22-2008 03:38 PM
Joshua B.
National Instruments
0 Kudos
Message 6 of 7
(6,146 Views)

Hi,

 

Can I add and replace static content into an existing NI Web service?

 

I need this functionality because I have to create a website from which the user can display some pictures. These pictures must be updated periodically Instead of updating them at real time when the user wants them. To do so I have to develop a VI to do the job, but I am wondering how can I transfer these images to a static folder in web server to be called by a url.  

 

Thank you very much

0 Kudos
Message 7 of 7
(5,264 Views)