12-17-2008 05:48 PM
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.
Solved! Go to Solution.
12-18-2008 04:11 PM
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
12-18-2008 05:36 PM
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.
12-19-2008 04:23 PM
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!
12-19-2008 05:08 PM
12-22-2008 03:37 PM - edited 12-22-2008 03:38 PM
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.
06-13-2012 09:33 AM
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