Hi again @jayD ,
I found a more or less “stripped down to the absolute minimum” demo on my harddrive I made about 10 years ago that shows, how to read a plc variable trough the embedded webserver using ajax and webservers ASP.
I used the jQuery framework inside this demo (a very old version, but that makes no difference for this use-case). Even if jQuery is not the framework you use, maybe the sample code helps a bit because it shows only the principle behind PLCs ASP and Ajax, nothing more → therefore the’re no fancy widgets or anything else inside: looks ugly, but does the job
Here’s the demo zip, some explanation follows:
VerySimpleAjaxDemo.zip (147.6 KB)
It’s a AS4.12 project, only containing 1 task with 3 variables.
The HTML, Javascript, and ASP data is located in the AS project root folder, subfolder “WEBROOT”:
Depending on where the demo is located on the harddrive, you have to change the webroot setting in the project. If you use “c:\projects”, it should be fine, if not please change the following setting to your needs:
How it works?
In the PLC, there’s a task hosting 3 variables changing their values.
In the webroot, the’re 3 files:
- index.html contains the needed HTML and also Javascript code (which is not a good coding style, but for a simple demo it’s better to explain what happens)
- demo_variables.json.asp contains the PLCs webserver ASP function “ReadPLC”, to get access to a variables value - therefore, the file has to end with .asp, because this file ending forces the webserver to parse the file before sending it and to check for ASP tags inside.
- jquery-1.11.1.min,js is the jQuery JS framework I use to make my own JS code easier.
index.html
- In line 11-13, the’re 3 DOM elements with a unique id, for example “id=idCounter” → knowing this id makes it possible to access this DOM element via JS code.
- Beginning at line 23, there’s my own JS function using the ajax call (in line 25 - $.ajax → jQuery syntax)
- line 26 - this ajax call requests the file “demo_ variables.json.asp” from the webserver
- line 27 - after sucessful completion, parses the response to a JSON list (because the content of “demo_variables.json.asp” is JSON coded)
- line 31 - uses the 1st element as DOM id, and the 2nd element as value, and updates the DOM elements ($(‘#idname’).text() method of jQuery)
- line 34 - and calls again after 1000ms the method for loading, parsing and updating the values
demo_variables.json.asp
Here, the JSON coded data is inside, using <%ReadPLC(“variablename”);%> as ASP method of the PLCs webserver to get variable values.
In the end, if everything works fine … if you open your webbrowser it should look like the following screenshot, and the marked values should change about every second.
I hope, this little demo shows a bit more concrete how it could be done by using Ajax and webservers ASP functions.
Best regards!